Skip to content

Commit

Permalink
gnrc_sixlowpan_frag: factor-out and rename fragmentation buffer
Browse files Browse the repository at this point in the history
The name `fragment_msg` or `frag_msg`/`msg_frag` always to me was a bit
misplaced, as it basically implements an asynchronous fragmentation
buffer and doesn't necessarily have anything to do with messages.
This change

1. changes the name to `fb` (for fragmentation buffer)
2. factors its code out to its own sub-module so it can be re-used by
   other 6LoWPAN fragmentation schemes like [Selective Fragment
   Recovery]

[Selective Fragment Recovery]: https://tools.ietf.org/html/draft-ietf-6lo-fragment-recovery-05
  • Loading branch information
miri64 committed Oct 24, 2019
1 parent 0838199 commit 54e2715
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 156 deletions.
6 changes: 6 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,21 @@ endif

ifneq (,$(filter gnrc_sixlowpan_frag,$(USEMODULE)))
USEMODULE += gnrc_sixlowpan
USEMODULE += gnrc_sixlowpan_frag_fb
USEMODULE += gnrc_sixlowpan_frag_rb
endif

ifneq (,$(filter gnrc_sixlowpan_frag_fb,$(USEMODULE)))
USEMODULE += core_msg
endif

ifneq (,$(filter gnrc_sixlowpan_frag_rb,$(USEMODULE)))
USEMODULE += xtimer
endif

ifneq (,$(filter gnrc_sixlowpan_frag_vrb,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += gnrc_sixlowpan_frag_fb
endif

ifneq (,$(filter gnrc_sixlowpan_iphc,$(USEMODULE)))
Expand Down
8 changes: 4 additions & 4 deletions sys/include/net/gnrc/sixlowpan/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ extern "C" {
/**
* @brief Number of datagrams that can be fragmented simultaneously
*
* This determines the number of @ref gnrc_sixlowpan_msg_frag_t instances
* This determines the number of @ref gnrc_sixlowpan_frag_fb_t instances
* available.
*
* @note Only applicable with
* [gnrc_sixlowpan_frag](@ref net_gnrc_sixlowpan_frag) module
* [gnrc_sixlowpan_frag_fb](@ref net_gnrc_sixlowpan_frag_fb) module
*/
#ifndef GNRC_SIXLOWPAN_MSG_FRAG_SIZE
#define GNRC_SIXLOWPAN_MSG_FRAG_SIZE (1U)
#ifndef GNRC_SIXLOWPAN_FRAG_FB_SIZE
#define GNRC_SIXLOWPAN_FRAG_FB_SIZE (1U)
#endif

/**
Expand Down
72 changes: 5 additions & 67 deletions sys/include/net/gnrc/sixlowpan/frag.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
#define NET_GNRC_SIXLOWPAN_FRAG_H

#include <inttypes.h>
#include <stdbool.h>

#include "byteorder.h"
#include "msg.h"
#include "net/gnrc/pkt.h"
#include "net/gnrc/netif/hdr.h"
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG_HINT
#include "net/gnrc/sixlowpan/frag/hint.h"
#endif /* MODULE_GNRC_SIXLOWPAN_FRAG_HINT */
#include "net/gnrc/sixlowpan/frag/fb.h"
#include "net/gnrc/sixlowpan/internal.h"
#include "net/ieee802154.h"
#include "net/sixlowpan.h"
Expand All @@ -47,35 +46,12 @@ extern "C" {
* @name Message types
* @{
*/
/**
* @brief Message type for passing one 6LoWPAN fragment down the network stack
*/
#define GNRC_SIXLOWPAN_MSG_FRAG_SND (0x0225)

/**
* @brief Message type for triggering garbage collection reassembly buffer
*/
#define GNRC_SIXLOWPAN_MSG_FRAG_GC_RBUF (0x0226)
/** @} */

/**
* @brief Definition of 6LoWPAN fragmentation type.
*/
typedef struct {
gnrc_pktsnip_t *pkt; /**< Pointer to the IPv6 packet to be fragmented */
uint16_t datagram_size; /**< Length of just the (uncompressed) IPv6 packet to be fragmented */
uint16_t tag; /**< Tag used for the fragment */
uint16_t offset; /**< Offset of the Nth fragment from the beginning of the
* payload datagram */
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG_HINT
/**
* @brief Hint for the size (smaller than link-layer PDU) for the next
* fragment to sent
*/
gnrc_sixlowpan_frag_hint_t hint;
#endif /* MODULE_GNRC_SIXLOWPAN_FRAG_HINT */
} gnrc_sixlowpan_msg_frag_t;

#if defined(MODULE_GNRC_SIXLOWPAN_FRAG_STATS) || DOXYGEN
/**
* @brief Statistics on fragmentation and reassembly
Expand All @@ -86,7 +62,7 @@ typedef struct {
unsigned rbuf_full; /**< counts the number of events where the
* reassembly buffer is full */
unsigned frag_full; /**< counts the number of events that there where
* no @ref gnrc_sixlowpan_msg_frag_t available */
* no @ref gnrc_sixlowpan_frag_fb_t available */
#if defined(MODULE_GNRC_SIXLOWPAN_FRAG_VRB) || DOXYGEN
unsigned vrb_full; /**< counts the number of events where the virtual
* reassembly buffer is full */
Expand All @@ -101,26 +77,18 @@ typedef struct {
gnrc_sixlowpan_frag_stats_t *gnrc_sixlowpan_frag_stats_get(void);
#endif

/**
* @brief Allocates a @ref gnrc_sixlowpan_msg_frag_t object
*
* @return A @ref gnrc_sixlowpan_msg_frag_t if available
* @return NULL, otherwise
*/
gnrc_sixlowpan_msg_frag_t *gnrc_sixlowpan_msg_frag_get(void);

/**
* @brief Sends a packet fragmented
*
* @pre `ctx != NULL`
* @pre gnrc_sixlowpan_msg_frag_t::pkt of @p ctx is equal to @p pkt or
* @pre gnrc_sixlowpan_frag_fb_t::pkt of @p ctx is equal to @p pkt or
* `pkt == NULL`.
*
* @param[in] pkt A packet. May be NULL.
* @param[in] ctx Message containing status of the 6LoWPAN fragmentation
* progress. Expected to be of type
* @ref gnrc_sixlowpan_msg_frag_t, with
* gnrc_sixlowpan_msg_frag_t set to @p pkt. Must not be
* @ref gnrc_sixlowpan_frag_fb_t, with
* gnrc_sixlowpan_frag_fb_t set to @p pkt. Must not be
* NULL.
* @param[in] page Current 6Lo dispatch parsing page.
*/
Expand All @@ -135,36 +103,6 @@ void gnrc_sixlowpan_frag_send(gnrc_pktsnip_t *pkt, void *ctx, unsigned page);
*/
void gnrc_sixlowpan_frag_recv(gnrc_pktsnip_t *pkt, void *ctx, unsigned page);

/**
* @brief Generate a new datagram tag for sending
*
* @return A new datagram tag.
*/
uint16_t gnrc_sixlowpan_frag_next_tag(void);

/**
* @brief Sends a message to pass a further fragment down the network stack
*
* @see GNRC_SIXLOWPAN_MSG_FRAG_SND
*
* @param[in] fragment_msg A @ref gnrc_sixlowpan_msg_frag_t object
*
* @return true, when the message was sent
* @return false when sending the message failed.
*/
static inline bool gnrc_sixlowpan_frag_send_msg(gnrc_sixlowpan_msg_frag_t *fragment_msg)
{
msg_t msg;

msg.content.ptr = fragment_msg;
msg.type = GNRC_SIXLOWPAN_MSG_FRAG_SND;
#ifdef TEST_SUITES
return (msg_try_send(&msg, gnrc_sixlowpan_get_pid()) > 0);
#else
return (msg_send_to_self(&msg) != 0);
#endif
}

#ifdef __cplusplus
}
#endif
Expand Down
106 changes: 106 additions & 0 deletions sys/include/net/gnrc/sixlowpan/frag/fb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2019 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @defgroup net_gnrc_sixlowpan_frag_fb 6LoWPAN fragmentation buffer
* @ingroup net_gnrc_sixlowpan_frag
* @brief Buffer for asynchronous 6LoWPAN fragmentation
* @{
*
* @file
* @brief Fragmentation buffer definitons
*
* @author Martine Lenders <m.lenders@fu-berlin.de>
*/
#ifndef NET_GNRC_SIXLOWPAN_FRAG_FB_H
#define NET_GNRC_SIXLOWPAN_FRAG_FB_H

#include <stdbool.h>
#include <stdint.h>

#include "msg.h"
#include "net/gnrc/pkt.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Message type for passing one 6LoWPAN fragment down the network stack
*/
#define GNRC_SIXLOWPAN_FRAG_FB_SND_MSG (0x0225)

/**
* @brief 6LoWPAN fragmentation buffer entry.
*/
typedef struct {
gnrc_pktsnip_t *pkt; /**< Pointer to the IPv6 packet to be fragmented */
uint16_t datagram_size; /**< Length of just the (uncompressed) IPv6 packet to be fragmented */
uint16_t tag; /**< Tag used for the fragment */
uint16_t offset; /**< Offset of the Nth fragment from the beginning of the
* payload datagram */
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG_HINT
/**
* @brief Hint for the size (smaller than link-layer PDU) for the next
* fragment to sent
*/
gnrc_sixlowpan_frag_hint_t hint;
#endif /* MODULE_GNRC_SIXLOWPAN_FRAG_HINT */
} gnrc_sixlowpan_frag_fb_t;

/**
* @brief Allocates a fragmentation buffer entry
*
* @return A fragmentation buffer entry if available
* @return NULL, otherwise
*/
gnrc_sixlowpan_frag_fb_t *gnrc_sixlowpan_frag_fb_get(void);

/**
* @brief Generate a new datagram tag for sending
*
* @return A new datagram tag.
*/
uint16_t gnrc_sixlowpan_frag_fb_next_tag(void);

#ifdef TEST_SUITES
#include "kernel_types.h"

/* can't include `net/sixlowpan.h` as this would create a cyclical include */
extern kernel_pid_t gnrc_sixlowpan_get_pid(void);
#endif

/**
* @brief Sends a message to pass a further fragment down the network stack
*
* @see GNRC_SIXLOWPAN_MSG_FRAG_SND
*
* @param[in] fragment_msg A @ref gnrc_sixlowpan_msg_frag_t object
*]
* @return true, when the message was sent
* @return false when sending the message failed.
*/
static inline bool gnrc_sixlowpan_frag_fb_send(gnrc_sixlowpan_frag_fb_t *fragment_msg)
{
msg_t msg;

msg.content.ptr = fragment_msg;
msg.type = GNRC_SIXLOWPAN_FRAG_FB_SND_MSG;
#ifdef TEST_SUITES
return (msg_try_send(&msg, gnrc_sixlowpan_get_pid()) > 0);
#else
return (msg_send_to_self(&msg) != 0);
#endif
}

#ifdef __cplusplus
}
#endif

#endif /* NET_GNRC_SIXLOWPAN_FRAG_FB_H */
/** @} */
3 changes: 3 additions & 0 deletions sys/net/gnrc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ endif
ifneq (,$(filter gnrc_sixlowpan_frag,$(USEMODULE)))
DIRS += network_layer/sixlowpan/frag
endif
ifneq (,$(filter gnrc_sixlowpan_frag_fb,$(USEMODULE)))
DIRS += network_layer/sixlowpan/frag/fb
endif
ifneq (,$(filter gnrc_sixlowpan_frag_rb,$(USEMODULE)))
DIRS += network_layer/sixlowpan/frag/rb
endif
Expand Down
3 changes: 3 additions & 0 deletions sys/net/gnrc/network_layer/sixlowpan/frag/fb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE := gnrc_sixlowpan_frag_fb

include $(RIOTBASE)/Makefile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @{
*
* @file
* @author Martine Lenders <m.lenders@fu-berlin.de>
*/

#include <stdint.h>

#include "net/gnrc/sixlowpan/config.h"
#include "net/gnrc/sixlowpan/frag/fb.h"
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG_STATS
#include "net/gnrc/sixlowpan/frag/stats.h"
#endif

static gnrc_sixlowpan_frag_fb_t _fbs[GNRC_SIXLOWPAN_FRAG_FB_SIZE];
static uint16_t _current_tag;

gnrc_sixlowpan_frag_fb_t *gnrc_sixlowpan_frag_fb_get(void)
{
for (unsigned i = 0; i < GNRC_SIXLOWPAN_FRAG_FB_SIZE; i++) {
if (_fbs[i].pkt == NULL) {
return &_fbs[i];
}
}
#ifdef MODULE_GNRC_SIXLOWPAN_FRAG_STATS
gnrc_sixlowpan_frag_stats_get()->frag_full++;
#endif
return NULL;
}

uint16_t gnrc_sixlowpan_frag_fb_next_tag(void)
{
return (++_current_tag);
}

/** @} */
Loading

0 comments on commit 54e2715

Please # to comment.