Loading...
Searching...
No Matches
netif.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-20 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
31
32#include <stddef.h>
33#include <stdint.h>
34#include <stdbool.h>
35
36#include "compiler_hints.h"
37#include "event.h"
38#include "msg.h"
39#include "net/gnrc/netapi.h"
40#include "net/gnrc/netif/conf.h"
42#include "net/gnrc/pkt.h"
43#include "net/ipv6/addr.h"
44#include "net/l2util.h"
45#include "net/ndp.h"
46#include "net/netdev.h"
47#include "net/netif.h"
48#include "net/netopt.h"
49#include "rmutex.h"
50#include "sched.h"
51
52#ifdef MODULE_GNRC_NETIF_BUS
53# include "msg_bus.h"
54#endif
55
56#if IS_USED(MODULE_GNRC_NETIF_LORAWAN)
58#endif
59
60#if IS_USED(MODULE_GNRC_NETIF_6LO)
61# include "net/gnrc/netif/6lo.h"
62#endif
63
64#if defined(MODULE_GNRC_NETIF_DEDUP) && (GNRC_NETIF_L2ADDR_MAXLEN > 0)
65# include "net/gnrc/netif/dedup.h"
66#endif
67
68#if IS_USED(MODULE_GNRC_NETIF_IPV6)
69# include "net/gnrc/netif/ipv6.h"
70#endif
71
72#if IS_USED(MODULE_GNRC_NETIF_PKTQ)
74#endif
75
76#ifdef MODULE_NETSTATS_L2
77# include "net/netstats.h"
78#endif
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
87#define GNRC_NETIF_EVQ_INDEX_PRIO_HIGH (0)
88
92#if IS_USED(MODULE_BHP_EVENT)
93#define GNRC_NETIF_EVQ_INDEX_PRIO_LOW (GNRC_NETIF_EVQ_INDEX_PRIO_HIGH + 1)
94#else
95#define GNRC_NETIF_EVQ_INDEX_PRIO_LOW GNRC_NETIF_EVQ_INDEX_PRIO_HIGH
96#endif
97
101#define GNRC_NETIF_EVQ_NUMOF (GNRC_NETIF_EVQ_INDEX_PRIO_LOW + 1)
102
106typedef enum {
107#ifdef MODULE_GNRC_IPV6
110#endif
111 GNRC_NETIF_BUS_NUMOF
113
131
136
140typedef struct {
145#if IS_USED(MODULE_NETSTATS_L2) || defined(DOXYGEN)
147#endif
148#if IS_USED(MODULE_GNRC_NETIF_LORAWAN) || defined(DOXYGEN)
150#endif
151#if IS_USED(MODULE_GNRC_NETIF_IPV6) || defined(DOXYGEN)
153#endif
154#if IS_USED(MODULE_GNRC_NETIF_BUS) || DOXYGEN
155 msg_bus_t bus[GNRC_NETIF_BUS_NUMOF];
156#endif
162 uint32_t flags;
171#if IS_USED(MODULE_NETDEV_NEW_API) || defined(DOXYGEN)
186#endif
187#if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
195
201 uint8_t l2addr_len;
202#if defined(MODULE_GNRC_NETIF_DEDUP) || DOXYGEN
209#endif
210#endif
211#if IS_USED(MODULE_GNRC_NETIF_6LO) || defined(DOXYGEN)
213#endif
214#if IS_USED(MODULE_GNRC_NETIF_PKTQ) || defined(DOXYGEN)
221#endif
226 uint8_t cur_hl;
227 uint8_t device_type;
230
247{
248 if (!IS_USED(MODULE_NETDEV_NEW_API) && !IS_USED(MODULE_NETDEV_LEGACY_API)) {
249 /* this should only happen for external netdevs or when no netdev is
250 * used (e.g. examples/networking/coap/gcoap can be used without any netdev, as still
251 * CoAP requests to ::1 can be send */
252 return true;
253 }
254
255 if (!IS_USED(MODULE_NETDEV_NEW_API)) {
256 return true;
257 }
258
259 if (!IS_USED(MODULE_NETDEV_LEGACY_API)) {
260 return false;
261 }
262
263 /* both legacy and new API netdevs in use, fall back to runtime test: */
264 return (netif->dev->driver->confirm_send == NULL);
265}
266
275static inline bool gnrc_netif_netdev_new_api(gnrc_netif_t *netif)
276{
277 return !gnrc_netif_netdev_legacy_api(netif);
278}
279
305 int (*init)(gnrc_netif_t *netif);
306
327 int (*send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);
328
345 gnrc_pktsnip_t *(*recv)(gnrc_netif_t *netif);
346
361 int (*get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt);
362
378 int (*set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt);
379
390 void (*msg_handler)(gnrc_netif_t *netif, msg_t *msg);
391};
392
402
420ACCESS(write_only, 2, 3)
421int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize,
422 char priority, const char *name, netdev_t *dev,
423 const gnrc_netif_ops_t *ops);
424
430unsigned gnrc_netif_numof(void);
431
443static inline bool gnrc_netif_highlander(void)
444{
445 return IS_USED(MODULE_GNRC_NETIF_SINGLE);
446}
447
457
467
489static inline int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif,
490 ipv6_addr_t *addrs,
491 size_t max_len)
492{
493 assert(netif != NULL);
494 assert(addrs != NULL);
495 assert(max_len >= sizeof(ipv6_addr_t));
496 return gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, addrs, max_len);
497}
498
520static inline int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif,
521 const ipv6_addr_t *addr, unsigned pfx_len,
522 uint8_t flags)
523{
524 assert(netif != NULL);
525 assert(addr != NULL);
526 assert((pfx_len > 0) && (pfx_len <= 128));
527 return gnrc_netapi_set(netif->pid, NETOPT_IPV6_ADDR,
528 ((pfx_len << 8U) | flags), addr,
529 sizeof(ipv6_addr_t));
530}
531
545static inline int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif,
546 const ipv6_addr_t *addr)
547{
548 assert(netif != NULL);
549 assert(addr != NULL);
551 0, addr, sizeof(ipv6_addr_t));
552}
553
574static inline int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif,
575 ipv6_addr_t *groups,
576 size_t max_len)
577{
578 assert(netif != NULL);
579 assert(groups != NULL);
580 assert(max_len >= sizeof(ipv6_addr_t));
581 return gnrc_netapi_get(netif->pid, NETOPT_IPV6_GROUP, 0, groups, max_len);
582}
583
598static inline int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif,
599 const ipv6_addr_t *group)
600{
601 assert(netif != NULL);
602 assert(group != NULL);
603 return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP, 0, group,
604 sizeof(ipv6_addr_t));
605}
606
620static inline int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif,
621 const ipv6_addr_t *group)
622{
623 assert(netif != NULL);
624 assert(group != NULL);
625 return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP_LEAVE, 0, group,
626 sizeof(ipv6_addr_t));
627}
628
638
651
664 const gnrc_netapi_opt_t *opt);
665
678
697static inline char *gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
698{
699 return l2util_addr_to_str(addr, addr_len, out);
700}
701
722static inline size_t gnrc_netif_addr_from_str(const char *str,
723 uint8_t out[GNRC_NETIF_L2ADDR_MAXLEN])
724{
726}
727
737static inline int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
738{
739 return gnrc_netapi_send(netif->pid, pkt);
740}
741
742#if defined(MODULE_GNRC_NETIF_BUS) || DOXYGEN
752 gnrc_netif_bus_t type)
753{
754 assert(type < GNRC_NETIF_BUS_NUMOF);
755 return &netif->bus[type];
756}
757
774 uint32_t timeout_ms);
775#endif /* MODULE_GNRC_NETIF_BUS */
776
777#ifdef __cplusplus
778}
779#endif
780
6LoWPAN definitions for Network interface API
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
Definitions low-level network driver interface.
Flag definitions for Network interface API.
IPv6 definitions for Network interface API.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:134
struct netdev netdev_t
Forward declaration for netdev struct.
Definition netdev.h:285
netdev_type_t
Driver types for netdev.
Definition netdev.h:303
static int gnrc_netapi_set(kernel_pid_t pid, netopt_t opt, uint16_t context, const void *data, size_t data_len)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_SET messages and parsing the returned GNRC_NETAPI_...
Definition netapi.h:283
static int gnrc_netapi_get(kernel_pid_t pid, netopt_t opt, uint16_t context, void *data, size_t max_len)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_GET messages and parsing the returned GNRC_NETAPI_...
Definition netapi.h:260
static int gnrc_netapi_send(kernel_pid_t pid, gnrc_pktsnip_t *pkt)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_SND messages.
Definition netapi.h:161
#define GNRC_NETIF_L2ADDR_MAXLEN
Maximum length of the link-layer address.
Definition conf.h:158
static int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif, ipv6_addr_t *addrs, size_t max_len)
Gets the (unicast on anycast) IPv6 address of an interface (if IPv6 is supported)
Definition netif.h:489
static size_t gnrc_netif_addr_from_str(const char *str, uint8_t out[GNRC_NETIF_L2ADDR_MAXLEN])
Parses a string of colon-separated hexadecimals to a hardware address.
Definition netif.h:722
static bool gnrc_netif_netdev_new_api(gnrc_netif_t *netif)
Check if the device belonging to the given netif uses the new netdev API.
Definition netif.h:275
static int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif, const ipv6_addr_t *group)
Leaves an IPv6 multicast group on an interface (if IPv6 is supported)
Definition netif.h:620
static int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif, const ipv6_addr_t *group)
Joins an IPv6 multicast group on an interface (if IPv6 is supported)
Definition netif.h:598
gnrc_netif_t * gnrc_netif_get_by_type(netdev_type_t type, uint8_t index)
Gets an interface by the netdev type (and index)
gnrc_ipv6_event_t
Event types for GNRC_NETIF_BUS_IPV6 per-interface message bus.
Definition netif.h:117
#define GNRC_NETIF_EVQ_NUMOF
Number of event queues.
Definition netif.h:101
static bool gnrc_netif_netdev_legacy_api(gnrc_netif_t *netif)
Check if the device belonging to the given netif uses the legacy netdev API.
Definition netif.h:246
gnrc_netif_t * gnrc_netif_iter(const gnrc_netif_t *prev)
Iterate over all network interfaces.
int gnrc_netif_set_from_netdev(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::set()
static char * gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
Converts a hardware address to a human readable string.
Definition netif.h:697
static int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a GNRC packet via a given gnrc_netif_t interface.
Definition netif.h:737
int gnrc_netif_default_init(gnrc_netif_t *netif)
Default operation for gnrc_netif_ops_t::init()
static int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif, const ipv6_addr_t *addr, unsigned pfx_len, uint8_t flags)
Adds an (unicast or anycast) IPv6 address to an interface (if IPv6 is supported)
Definition netif.h:520
unsigned gnrc_netif_numof(void)
Get number of network interfaces actually allocated.
bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif, uint32_t timeout_ms)
Wait for a global address to become available.
int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize, char priority, const char *name, netdev_t *dev, const gnrc_netif_ops_t *ops)
Creates a network interface.
gnrc_netif_bus_t
Per-Interface Event Message Buses.
Definition netif.h:106
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
static msg_bus_t * gnrc_netif_get_bus(gnrc_netif_t *netif, gnrc_netif_bus_t type)
Get a message bus of a given gnrc_netif_t interface.
Definition netif.h:751
struct gnrc_netif_ops gnrc_netif_ops_t
Operations to an interface.
Definition netif.h:135
int gnrc_netif_get_from_netdev(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::get()
static int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif, ipv6_addr_t *groups, size_t max_len)
Gets the IPv6 multicast groups an interface is joined to (if IPv6 is supported)
Definition netif.h:574
static int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif, const ipv6_addr_t *addr)
Removes a (unicast or anycast) IPv6 address from an interface (if IPv6 is supported)
Definition netif.h:545
static bool gnrc_netif_highlander(void)
Check if there can only be one gnrc_netif_t interface.
Definition netif.h:443
void gnrc_netif_init_devs(void)
Initialize all available network interfaces.
@ GNRC_IPV6_EVENT_ADDR_VALID
Address becomes valid.
Definition netif.h:129
@ GNRC_NETIF_BUS_IPV6
provides gnrc_ipv6_event_t messages to subscribers
Definition netif.h:108
struct gnrc_pktsnip gnrc_pktsnip_t
Type to represent parts (either headers or payload) of a packet, called snips.
size_t l2util_addr_from_str_sized(const char *str, void *addr, size_t addr_size)
Parses a string of colon-separated hexadecimals to a hardware address.
char * l2util_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
Converts a hardware address to a human readable string.
@ NETOPT_IPV6_GROUP
(ipv6_addr_t) get IPv6 multicast groups of an interface as array of ipv6_addr_t or join an IPv6 multi...
Definition netopt.h:163
@ NETOPT_IPV6_GROUP_LEAVE
(ipv6_addr_t) Leave an IPv6 multicast group on an interface
Definition netopt.h:167
@ NETOPT_IPV6_ADDR
(ipv6_addr_t[]) get IPv6 addresses of an interface as array of ipv6_addr_t or add an IPv6 address as ...
Definition netopt.h:135
@ NETOPT_IPV6_ADDR_REMOVE
(ipv6_addr_t) Removes an IPv6 address from an interface
Definition netopt.h:139
struct PTRTAG event_queue_t
event queue structure
struct event event_t
event structure forward declaration
Definition event.h:135
Definition of net statistics.
Definitions for IPv6 addresses.
Link-layer helper function definitions.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition modules.h:67
Messaging Bus API for inter process message broadcast.
IPv6 neighbor discovery message type definitions.
Send queue for type definitions
General definitions for network packets and their helper functions.
Generic interface to communicate with GNRC modules.
Configuration macros for Network interface API.
#define GNRC_NETIF_MSG_QUEUE_SIZE
Message queue size for network interface threads.
Definition conf.h:182
LoRaWAN adaption for Network interface API.
Common network interface API definitions.
Definition of global configuration options.
Recursive Mutex for thread synchronization.
Data structure to be send for setting (GNRC_NETAPI_MSG_TYPE_SET) and getting (GNRC_NETAPI_MSG_TYPE_GE...
Definition netapi.h:108
6Lo component of gnrc_netif_t
Definition 6lo.h:52
Structure to store information on the last broadcast packet received.
Definition dedup.h:41
IPv6 component for gnrc_netif_t.
Definition ipv6.h:78
GNRC LoRaWAN interface descriptor.
Definition lorawan.h:37
int(* init)(gnrc_netif_t *netif)
Initializes and registers network interface.
Definition netif.h:305
int(* set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Sets an option from the network interface.
Definition netif.h:378
int(* send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a packet over the network interface.
Definition netif.h:327
void(* msg_handler)(gnrc_netif_t *netif, msg_t *msg)
Message handler for network interface.
Definition netif.h:390
int(* get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Gets an option from the network interface.
Definition netif.h:361
A packet queue for Network interface API with a de-queue timer.
Definition type.h:36
Representation of a network interface.
Definition netif.h:140
gnrc_netif_lorawan_t lorawan
LoRaWAN component.
Definition netif.h:149
msg_bus_t bus[GNRC_NETIF_BUS_NUMOF]
Event Message Bus.
Definition netif.h:155
event_queue_t evq[GNRC_NETIF_EVQ_NUMOF]
Event queue for asynchronous events.
Definition netif.h:166
rmutex_t mutex
Mutex of the interface.
Definition netif.h:144
netstats_t stats
transceiver's statistics
Definition netif.h:146
uint32_t flags
Flags for the interface.
Definition netif.h:162
const gnrc_netif_ops_t * ops
Operations of the network interface.
Definition netif.h:142
uint8_t device_type
Device type.
Definition netif.h:227
gnrc_netif_6lo_t sixlo
6Lo component
Definition netif.h:212
uint8_t cur_hl
Current hop-limit for out-going packets.
Definition netif.h:226
netif_t netif
network interface descriptor
Definition netif.h:141
uint8_t l2addr_len
Length in bytes of gnrc_netif_t::l2addr.
Definition netif.h:201
kernel_pid_t pid
PID of the network interface's thread.
Definition netif.h:228
event_t event_isr
ISR event for the network device.
Definition netif.h:170
uint8_t l2addr[GNRC_NETIF_L2ADDR_MAXLEN]
The link-layer address currently used as the source address on this interface.
Definition netif.h:194
gnrc_netif_dedup_t last_pkt
Last received packet information.
Definition netif.h:208
gnrc_netif_ipv6_t ipv6
IPv6 component.
Definition netif.h:152
msg_t msg_queue[GNRC_NETIF_MSG_QUEUE_SIZE]
Message queue for the netif thread.
Definition netif.h:225
netdev_t * dev
Network device of the network interface.
Definition netif.h:143
gnrc_pktsnip_t * tx_pkt
Outgoing frame that is currently transmitted.
Definition netif.h:185
event_t event_tx_done
TX done event for the network device.
Definition netif.h:177
gnrc_netif_pktq_t send_queue
Packet queue for sending.
Definition netif.h:220
A message bus is just a list of subscribers.
Definition msg_bus.h:50
Describes a message object which can be sent between threads.
Definition msg.h:193
const struct netdev_driver * driver
ptr to that driver's interface.
Definition netdev.h:364
Network interface descriptor.
Definition netif.h:71
Global statistics struct.
Definition netstats.h:59
Mutex structure.
Definition rmutex.h:34
Data type to represent an IPv6 address.
Definition addr.h:67