Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
virtual.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2021 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#ifndef ZEPHYR_INCLUDE_NET_VIRTUAL_H_
12#define ZEPHYR_INCLUDE_NET_VIRTUAL_H_
13
14#include <kernel.h>
15#include <zephyr/types.h>
16#include <stdbool.h>
17#include <sys/atomic.h>
18
19#include <net/net_ip.h>
20#include <net/net_pkt.h>
21
22#include <sys/util.h>
23#include <net/net_if.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
40
42 /* Marker for capabilities - must be at the end of the enum.
43 * It is here because the capability list cannot be empty.
44 */
45 VIRTUAL_INTERFACE_NUM_CAPS
47};
48
51enum virtual_interface_config_type {
52 VIRTUAL_INTERFACE_CONFIG_TYPE_PEER_ADDRESS,
53 VIRTUAL_INTERFACE_CONFIG_TYPE_MTU,
54};
55
56struct virtual_interface_config {
57 sa_family_t family;
58 union {
59 struct in_addr peer4addr;
60 struct in6_addr peer6addr;
61 int mtu;
62 };
63};
64
65#if defined(CONFIG_NET_L2_VIRTUAL)
66#define VIRTUAL_MAX_NAME_LEN CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
67#else
68#define VIRTUAL_MAX_NAME_LEN 0
69#endif
77 struct net_if_api iface_api;
78
80 enum virtual_interface_caps (*get_capabilities)(struct net_if *iface);
81
83 int (*start)(const struct device *dev);
84
86 int (*stop)(const struct device *dev);
87
89 int (*send)(struct net_if *iface, struct net_pkt *pkt);
90
92 enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
93
99 enum net_verdict (*input)(struct net_if *input_iface,
100 struct net_if *iface,
101 struct net_addr *remote_addr,
102 struct net_pkt *pkt);
103
105 int (*attach)(struct net_if *virtual_iface, struct net_if *iface);
106
108 int (*set_config)(struct net_if *iface,
109 enum virtual_interface_config_type type,
110 const struct virtual_interface_config *config);
111
113 int (*get_config)(struct net_if *iface,
114 enum virtual_interface_config_type type,
115 struct virtual_interface_config *config);
116};
117
118/* Make sure that the network interface API is properly setup inside
119 * Virtual API struct (it is the first one).
120 */
121BUILD_ASSERT(offsetof(struct virtual_interface_api, iface_api) == 0);
122
127 /* Keep track of contexts */
128 sys_snode_t node;
129
130 /* My virtual network interface */
131 struct net_if *virtual_iface;
139 struct net_if *iface;
140
145
148
151
153 char name[VIRTUAL_MAX_NAME_LEN];
154};
155
165int net_virtual_interface_attach(struct net_if *virtual_iface,
166 struct net_if *iface);
167
177struct net_if *net_virtual_get_iface(struct net_if *iface);
178
188char *net_virtual_get_name(struct net_if *iface, char *buf, size_t len);
189
196void net_virtual_set_name(struct net_if *iface, const char *name);
197
207 enum net_l2_flags flags);
208
218enum net_verdict net_virtual_input(struct net_if *input_iface,
219 struct net_addr *remote_addr,
220 struct net_pkt *pkt);
221
230#if defined(CONFIG_NET_L2_VIRTUAL)
231void net_virtual_init(struct net_if *iface);
232#else
233static inline void net_virtual_init(struct net_if *iface)
234{
235 ARG_UNUSED(iface);
236}
237#endif
238
245#if defined(CONFIG_NET_L2_VIRTUAL)
246void net_virtual_disable(struct net_if *iface);
247#else
248static inline void net_virtual_disable(struct net_if *iface)
249{
250 ARG_UNUSED(iface);
251}
252#endif
253
254#define VIRTUAL_L2_CTX_TYPE struct virtual_interface_context
255
263static inline enum virtual_interface_caps
264net_virtual_get_iface_capabilities(struct net_if *iface)
265{
266 const struct virtual_interface_api *virt =
268
269 if (!virt->get_capabilities) {
270 return (enum virtual_interface_caps)0;
271 }
272
273 return virt->get_capabilities(iface);
274}
275
276#define Z_NET_VIRTUAL_INTERFACE_INIT(node_id, dev_name, drv_name, \
277 init_fn, pm_control_fn, data, cfg, \
278 prio, api, mtu) \
279 Z_NET_DEVICE_INIT(node_id, dev_name, drv_name, init_fn, \
280 pm_control_fn, data, cfg, prio, api, \
281 VIRTUAL_L2, NET_L2_GET_CTX_TYPE(VIRTUAL_L2), \
282 mtu)
308#define NET_VIRTUAL_INTERFACE_INIT(dev_name, drv_name, init_fn, \
309 pm_control_fn, \
310 data, cfg, prio, api, mtu) \
311 Z_NET_VIRTUAL_INTERFACE_INIT(DT_INVALID_NODE, dev_name, \
312 drv_name, init_fn, pm_control_fn, \
313 data, cfg, prio, api, mtu)
314
319#ifdef __cplusplus
320}
321#endif
322
323#endif /* ZEPHYR_INCLUDE_NET_VIRTUAL_H_ */
unsigned short int sa_family_t
Definition: net_ip.h:158
net_verdict
Net Verdict.
Definition: net_core.h:97
static const struct device * net_if_get_device(struct net_if *iface)
Get an network interface's device.
Definition: net_if.h:606
net_l2_flags
Definition: net_l2.h:33
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
void net_virtual_set_name(struct net_if *iface, const char *name)
Set the name of the virtual network interface L2.
enum net_l2_flags net_virtual_set_flags(struct net_if *iface, enum net_l2_flags flags)
Set the L2 flags of the virtual network interface.
int net_virtual_interface_attach(struct net_if *virtual_iface, struct net_if *iface)
Attach virtual network interface to the given network interface.
enum net_verdict net_virtual_input(struct net_if *input_iface, struct net_addr *remote_addr, struct net_pkt *pkt)
Feed the IP pkt to stack if tunneling is enabled.
virtual_interface_caps
Definition: virtual.h:37
char * net_virtual_get_name(struct net_if *iface, char *buf, size_t len)
Return the name of the virtual network interface L2.
struct net_if * net_virtual_get_iface(struct net_if *iface)
Return network interface related to this virtual network interface. The returned network interface is...
@ VIRTUAL_INTERFACE_IPIP
Definition: virtual.h:39
flags
Definition: http_parser.h:131
Public API for network interface.
IPv6 and IPv4 definitions.
Network packet buffer descriptor API.
struct _snode sys_snode_t
Definition: slist.h:33
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
Definition: net_ip.h:139
Definition: net_ip.h:148
Network Interface structure.
Definition: net_if.h:468
Hardware link address structure.
Definition: net_linkaddr.h:90
Network packet.
Definition: net_pkt.h:62
Definition: virtual.h:72
int(* stop)(const struct device *dev)
Definition: virtual.h:86
enum net_verdict(* input)(struct net_if *input_iface, struct net_if *iface, struct net_addr *remote_addr, struct net_pkt *pkt)
Definition: virtual.h:99
int(* get_config)(struct net_if *iface, enum virtual_interface_config_type type, struct virtual_interface_config *config)
Definition: virtual.h:113
int(* set_config)(struct net_if *iface, enum virtual_interface_config_type type, const struct virtual_interface_config *config)
Definition: virtual.h:108
enum net_verdict(* recv)(struct net_if *iface, struct net_pkt *pkt)
Definition: virtual.h:92
int(* send)(struct net_if *iface, struct net_pkt *pkt)
Definition: virtual.h:89
int(* attach)(struct net_if *virtual_iface, struct net_if *iface)
Definition: virtual.h:105
enum virtual_interface_caps(* get_capabilities)(struct net_if *iface)
Definition: virtual.h:80
struct net_if_api iface_api
Definition: virtual.h:77
int(* start)(const struct device *dev)
Definition: virtual.h:83
Definition: virtual.h:125
enum net_l2_flags virtual_l2_flags
Definition: virtual.h:144
struct net_if * iface
Definition: virtual.h:139
char name[VIRTUAL_MAX_NAME_LEN]
Definition: virtual.h:153
bool is_init
Definition: virtual.h:147
struct net_linkaddr_storage lladdr
Definition: virtual.h:150
Misc utilities.