Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions drivers/include/net/netdev/ieee802154.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ int netdev_ieee802154_set(netdev_ieee802154_t *dev, netopt_t opt, const void *va
*/
int netdev_ieee802154_dst_filter(netdev_ieee802154_t *dev, const uint8_t *mhr);

typedef struct {
uint8_t *psdu;
size_t psdu_len;
uint8_t lqi;
int16_t rssi;
void *ctx;
} ieee802154_phy_ind_t;

typedef struct {
iolist_t msdu;
uint8_t *src;
size_t src_len;
uint8_t *dst;
size_t dst_len;
uint8_t frame_pending;
} ieee802154_mac_ind_t;

void ieee802154_rf_rx_done(netdev_t *netdev);
void ieee802154_mac_recv(netdev_ieee802154_t *dev, ieee802154_phy_ind_t *ind);
void ieee802154_mac_ind(netdev_ieee802154_t *netdev, ieee802154_mac_ind_t *ind, ieee802154_phy_ind_t *phy_ind);
void ieee802154_phy_ind(netdev_t *netdev, ieee802154_phy_ind_t *ind);
int ieee802154_mac_send(netdev_ieee802154_t *netdev, iolist_t *msdu, int frame_pending,
uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len);
Comment on lines +197 to +219

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also missing doc here ;-)

Comment on lines +214 to +219

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these functions are implemented in sys, I guess the better place for them and the structs would be rather sys/include/net/ieee802154/mac.h and sys/include/net/ieee802154/phy.h respectively

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly. I think I added a comment somewhere that I added it in the netdev files because there were some circular dependencies :(. But the idea is to put them exactly where you describe



#ifdef __cplusplus
}
#endif
Expand Down
58 changes: 58 additions & 0 deletions sys/include/net/netbuf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2019 HAW Hamburg
*
* 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_netbuf Network buffer abstraction
* @ingroup net
* @brief Provides an abstraction for network stack buffers
* @{
*
* @file
* @brief Network buffer abstraction definition
*
* @author Jose I. Alamos <jose.alamos@haw-hamburg.de>
*/
#ifndef NET_NETBUF_H
#define NET_NETBUF_H

#include <stdbool.h>
#include "iolist.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Get a network buffer with a given size.
*
* @note Supposed to be implemented by the networking module.
*
* @param[in] size size of the buffer to be allocated.
* @param[out] ctx allocation context.
*
* @return pointer to the buffer.
* @return packet context in @p ctx.
* @return NULL on failure (e.g there's not enough memory for allocation)
*/
void *netbuf_get(size_t size, void **ctx);

/**
* @brief Free the resources of a network buffer
*
* @note Supposed to be implemented by the networking module.
*
* @param[in] ctx pointer to the allocation context.
*/
void netbuf_free(void *ctx);

#ifdef __cplusplus
}
#endif

#endif /* NET_NETBUF_H */
/** @} */
16 changes: 16 additions & 0 deletions sys/include/net/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "list.h"
#include "net/netopt.h"
#include "net/netbuf.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This include shouldn't be needed.


#ifdef __cplusplus
extern "C" {
Expand All @@ -58,6 +59,21 @@ typedef struct {
list_node_t node; /**< Pointer to the next interface */
} netif_t;

typedef struct {
iolist_t msdu;
void *ctx;
uint8_t *src_l2addr;
uint8_t *dst_l2addr;
int16_t rssi;
uint8_t lqi;
uint8_t src_l2addr_len;
uint8_t dst_l2addr_len;
uint8_t flags;
} netif_pkt_t;


int netif_recv(netif_t *netif, netif_pkt_t *pkt);
Comment on lines +62 to +75

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a POC, I know, but I'm missing some doc.


/**
* @brief Iterator for the interfaces
*
Expand Down
67 changes: 67 additions & 0 deletions sys/net/gnrc/netif/_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,78 @@
#include <string.h>

#include "fmt.h"
#include "net/gnrc.h"
#include "net/gnrc/netapi.h"
#include "net/gnrc/netif/internal.h"

#include "net/netif.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

/* GNRC implementation of netif_recv.
*
* This function converts the `netif_pkt` representation
* into a `gnrc_pkt` representation. Note that the
* netbuf implementation of GNRC uses the gnrc_pktsnip_t
* pointer as netbuf context, so we can get fetch the packet
* from there.
*
* Also, we need to create the gnrc_netif_hdr to pass the
* L2 metadata to the upper layers. And also remove the
* L2 header.
*
* Note that if a PHY layer didn't call the netbuf allocation
* function (e.g the PHY layer had a static framebuffer like
* the ESP-WIFI implementation), it's possible to set the
* allocation context (netif_pkt->ctx) to NULL.
* So, it's possible to only allocate the MSDU
* and not the full packet!! This would reduce the number
* of memcpy in those cases.
*/
int netif_recv(netif_t *netif, netif_pkt_t *pkt)
{
gnrc_pktsnip_t *gnrc_pkt = pkt->ctx;
gnrc_netif_t *gnrc_netif = (gnrc_netif_t*) netif;
netdev_t *dev = gnrc_netif->dev;

gnrc_pktsnip_t *netif_snip = gnrc_netif_hdr_build(pkt->src_l2addr, pkt->src_l2addr_len, pkt->dst_l2addr, pkt->dst_l2addr_len);

if (netif_snip == NULL) {
DEBUG("_recv_ieee802154: no space left in packet buffer\n");
gnrc_pktbuf_release(gnrc_pkt);
return 0;
}

/* We can avoid the following logic if we know the PHY layer
* didn't allocate the packet (see the description of this function).
*
* In that case, we would need to call
* `gnrc_pktbuf_add(pkt->msdu.iol_base, pkt->msdu.iol_len, GNRC_NETTYPE_UNDEF)`
* instead of removing the snip and reallocating data.
*/
gnrc_pktsnip_t *ieee802154_hdr = gnrc_pktbuf_mark(gnrc_pkt, ((uint8_t*) pkt->msdu.iol_base) - ((uint8_t*) gnrc_pkt->data), GNRC_NETTYPE_UNDEF);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what is happening here... Why the pointer arithmetic? Please add at least a comment about that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indication contains a pointer to the MSDU (and length).
In most network stacks it's possible to do:

the_network_stack_input(pkt->msdu.iol_base, pkt->msdu.iol_len);

However, in GNRC it's required to mark the pkt before passing it to the network stack.
So what I'm doing there is removing the IEEE802.15.4 header (all bytes between the beginning of the GNRC packet and the MSDU) plus footer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this seems like a misuse of gnrc_pktbuf_mark then. This is purely meant to mark a header, nothing else, sot he function should just receive the MAC header length as an input value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sot he function should just receive the MAC header length as an input value.
which function do you mean?


gnrc_pktbuf_remove_snip(gnrc_pkt, ieee802154_hdr);
gnrc_pktbuf_realloc_data(gnrc_pkt, pkt->msdu.iol_len);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a reallocation necessary here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some radios don't process FCS. Thus, it's necessary to remove it before passing it to the stack.
There might be better ways to do it though, I agree

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to add functions like gnrc_pktsnip_remove_hdr() or gnrc_pktsnip_remove_footer()?

@miri64 miri64 Nov 28, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some radios don't process FCS. Thus, it's necessary to remove it before passing it to the stack.

Ok. If it is just removing the trailing FCS, this is the correct usage. No need to add extra functions for that.

@miri64 miri64 Nov 28, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(But maybe a comment would be nice ;-)).

gnrc_netif_hdr_t *hdr = netif_snip->data;
hdr->lqi = pkt->lqi;
hdr->rssi = pkt->rssi;
gnrc_netif_hdr_set_netif(hdr, (gnrc_netif_t*) netif);
dev->driver->get(dev, NETOPT_PROTO, &gnrc_pkt->type, sizeof(gnrc_pkt->type));

hdr->flags |= pkt->flags;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the flags guaranteed to be transparent? If yes, what's the reasoning for that?


LL_APPEND(gnrc_pkt, netif_snip);

if (!gnrc_netapi_dispatch_receive(gnrc_pkt->type, GNRC_NETREG_DEMUX_CTX_ALL, gnrc_pkt))
{
return 0;
}

return 1;
}

int netif_get_name(netif_t *iface, char *name)
{
gnrc_netif_t *netif = (gnrc_netif_t*) iface;
Expand Down
23 changes: 22 additions & 1 deletion sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "sched.h"
#include "xtimer.h"

#include "net/netbuf.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netif/internal.h"

Expand All @@ -46,6 +47,25 @@ static void _configure_netdev(netdev_t *dev);
static void *_gnrc_netif_thread(void *args);
static void _event_cb(netdev_t *dev, netdev_event_t event);

/* GNRC implementation of the netbuf_get function */
void *netbuf_get(size_t size, void **ctx)
{
gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, NULL, size, GNRC_NETTYPE_UNDEF);

if(pkt) {
*ctx = pkt;
return pkt->data;
}

return NULL;
}

/* GNRC implementation of the netbuf_free function */
void netbuf_free(void *netbuf)
{
gnrc_pktbuf_release(netbuf);
}

gnrc_netif_t *gnrc_netif_create(char *stack, int stacksize, char priority,
const char *name, netdev_t *netdev,
const gnrc_netif_ops_t *ops)
Expand Down Expand Up @@ -1332,6 +1352,7 @@ void gnrc_netif_default_init(gnrc_netif_t *netif)
#endif
}

extern int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move the function here proper and add its declaration to net/gnrc/netif.h?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was too lazy for doing that :). But of course in the final version that would be in the right header

static void *_gnrc_netif_thread(void *args)
{
gnrc_netapi_opt_t *opt;
Expand Down Expand Up @@ -1388,7 +1409,7 @@ static void *_gnrc_netif_thread(void *args)
break;
case GNRC_NETAPI_MSG_TYPE_SND:
DEBUG("gnrc_netif: GNRC_NETDEV_MSG_TYPE_SND received\n");
res = netif->ops->send(netif, msg.content.ptr);
res = gnrc_netif_send(netif, msg.content.ptr);
if (res < 0) {
DEBUG("gnrc_netif: error sending packet %p (code: %i)\n",
msg.content.ptr, res);
Expand Down
Loading