Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
virtual.h
Go to the documentation of this file.
1
4
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 <zephyr/kernel.h>
15#include <zephyr/types.h>
16#include <stdbool.h>
17#include <zephyr/sys/atomic.h>
18
19#include <zephyr/net/net_ip.h>
20#include <zephyr/net/net_pkt.h>
21
22#include <zephyr/sys/util.h>
23#include <zephyr/net/net_if.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
37
42
45
48
51
53 /* Marker for capabilities - must be at the end of the enum.
54 * It is here because the capability list cannot be empty.
55 */
56 VIRTUAL_INTERFACE_NUM_CAPS
58};
59
61
62enum virtual_interface_config_type {
63 VIRTUAL_INTERFACE_CONFIG_TYPE_PEER_ADDRESS,
64 VIRTUAL_INTERFACE_CONFIG_TYPE_MTU,
65 VIRTUAL_INTERFACE_CONFIG_TYPE_LINK_TYPE,
66 VIRTUAL_INTERFACE_CONFIG_TYPE_PRIVATE_KEY,
67 VIRTUAL_INTERFACE_CONFIG_TYPE_PUBLIC_KEY,
68};
69
70struct virtual_interface_link_types {
71 int count;
72 uint16_t type[COND_CODE_1(CONFIG_NET_CAPTURE_COOKED_MODE,
73 (CONFIG_NET_CAPTURE_COOKED_MODE_MAX_LINK_TYPES),
74 (1))];
75};
76
77#if !defined(NET_VIRTUAL_MAX_PUBLIC_KEY_LEN)
78#define NET_VIRTUAL_MAX_PUBLIC_KEY_LEN 32U
79#endif
80
81struct virtual_interface_config {
82 net_sa_family_t family;
83 union {
84 struct net_in_addr peer4addr;
85 struct net_in6_addr peer6addr;
86 int mtu;
87 struct virtual_interface_link_types link_types;
88 struct {
89 size_t len;
90 uint8_t *data;
91 } private_key;
92 struct {
93 size_t len;
94 uint8_t data[NET_VIRTUAL_MAX_PUBLIC_KEY_LEN];
95 } public_key;
96 };
97};
98
99#if defined(CONFIG_NET_L2_VIRTUAL)
100#define VIRTUAL_MAX_NAME_LEN CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
101#else
102#define VIRTUAL_MAX_NAME_LEN 0
103#endif
105
112 struct net_if_driver_api iface_api;
113
115 enum virtual_interface_caps (*get_capabilities)(struct net_if *iface);
116
118 int (*start)(const struct device *dev);
119
121 int (*stop)(const struct device *dev);
122
124 int (*send)(struct net_if *iface, struct net_pkt *pkt);
125
132 enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
133
135 int (*attach)(struct net_if *virtual_iface, struct net_if *iface);
136
138 int (*set_config)(struct net_if *iface,
139 enum virtual_interface_config_type type,
140 const struct virtual_interface_config *config);
141
143 int (*get_config)(struct net_if *iface,
144 enum virtual_interface_config_type type,
145 struct virtual_interface_config *config);
146};
147
149DEVICE_API_EXTENDS(virtual_interface, net_if, iface_api);
151
156 /* Keep track of contexts */
157 sys_snode_t node;
158
159 /* My virtual network interface */
160 struct net_if *virtual_iface;
162
168 struct net_if *iface;
169
174
177
180
182 char name[VIRTUAL_MAX_NAME_LEN];
183};
184
194int net_virtual_interface_attach(struct net_if *virtual_iface,
195 struct net_if *iface);
196
206struct net_if *net_virtual_get_iface(struct net_if *iface);
207
217char *net_virtual_get_name(struct net_if *iface, char *buf, size_t len);
218
225void net_virtual_set_name(struct net_if *iface, const char *name);
226
236 enum net_l2_flags flags);
237
247enum net_verdict net_virtual_input(struct net_if *input_iface,
248 struct net_addr *remote_addr,
249 struct net_pkt *pkt);
250
252
259#if defined(CONFIG_NET_L2_VIRTUAL)
260void net_virtual_init(struct net_if *iface);
261#else
262static inline void net_virtual_init(struct net_if *iface)
263{
264 ARG_UNUSED(iface);
265}
266#endif
267
274#if defined(CONFIG_NET_L2_VIRTUAL)
275void net_virtual_disable(struct net_if *iface);
276#else
277static inline void net_virtual_disable(struct net_if *iface)
278{
279 ARG_UNUSED(iface);
280}
281#endif
282
289#if defined(CONFIG_NET_L2_VIRTUAL)
290void net_virtual_enable(struct net_if *iface);
291#else
292static inline void net_virtual_enable(struct net_if *iface)
293{
294 ARG_UNUSED(iface);
295}
296#endif
297
298#define VIRTUAL_L2_CTX_TYPE struct virtual_interface_context
299
307static inline enum virtual_interface_caps
308net_virtual_get_iface_capabilities(struct net_if *iface)
309{
310 const struct device *dev = net_if_get_device(iface);
311 const struct virtual_interface_driver_api *virt;
312
313 NET_ASSERT(dev != NULL);
314
315 virt = (const struct virtual_interface_driver_api *)dev->api;
316
317 NET_ASSERT(virt != NULL);
318
319 if (!virt->get_capabilities) {
320 return (enum virtual_interface_caps)0;
321 }
322
323 return virt->get_capabilities(iface);
324}
325
326#define Z_NET_VIRTUAL_INTERFACE_INIT(node_id, dev_id, name, init_fn, \
327 pm, data, config, prio, api, mtu) \
328 Z_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data, \
329 config, prio, api, VIRTUAL_L2, \
330 NET_L2_GET_CTX_TYPE(VIRTUAL_L2), mtu)
331
332#define Z_NET_VIRTUAL_INTERFACE_INIT_INSTANCE(node_id, dev_id, name, \
333 inst, init_fn, pm, data, \
334 config, prio, api, mtu) \
335 Z_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, inst, \
336 init_fn, pm, data, \
337 config, prio, api, VIRTUAL_L2, \
338 NET_L2_GET_CTX_TYPE(VIRTUAL_L2), mtu)
340
362#define NET_VIRTUAL_INTERFACE_INIT(dev_id, name, init_fn, pm, data, \
363 config, prio, api, mtu) \
364 Z_NET_VIRTUAL_INTERFACE_INIT(DT_INVALID_NODE, dev_id, name, \
365 init_fn, pm, data, config, prio, \
366 api, mtu)
367
390#define NET_VIRTUAL_INTERFACE_INIT_INSTANCE(dev_id, name, inst, \
391 init_fn, pm, data, \
392 config, prio, api, mtu) \
393 Z_NET_VIRTUAL_INTERFACE_INIT_INSTANCE(DT_INVALID_NODE, dev_id, \
394 name, inst, \
395 init_fn, pm, data, config, \
396 prio, api, mtu)
397
401
402#ifdef __cplusplus
403}
404#endif
405
406#endif /* ZEPHYR_INCLUDE_NET_VIRTUAL_H_ */
Header file for the Atomic operations API.
#define DEVICE_API_EXTENDS(_child, _parent, _member)
Declare that API class _child is an extension of device API class _parent.
Definition device.h:1470
unsigned short int net_sa_family_t
Socket address family type.
Definition net_ip.h:169
net_verdict
Net Verdict.
Definition net_core.h:84
static const struct device * net_if_get_device(struct net_if *iface)
Get an network interface's device.
Definition net_if.h:1205
net_l2_flags
L2 flags.
Definition net_l2.h:37
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition util_macro.h:221
#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
Virtual interface capabilities.
Definition virtual.h:39
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.
@ VIRTUAL_INTERFACE_IPIP
IPIP tunnel.
Definition virtual.h:41
@ VIRTUAL_INTERFACE_VPN
VPN interface.
Definition virtual.h:50
@ VIRTUAL_INTERFACE_BRIDGE
Virtual Ethernet bridge interface.
Definition virtual.h:47
@ VIRTUAL_INTERFACE_VLAN
Virtual LAN interface (VLAN).
Definition virtual.h:44
Public kernel APIs.
Public API for network interface.
IPv6 and IPv4 definitions.
Network packet buffer descriptor API.
flags
Definition parser.h:86
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:549
Network Interface structure.
Definition net_if.h:875
Hardware link address structure.
Definition net_linkaddr.h:83
Network packet.
Definition net_pkt.h:119
Virtual L2 context that is needed to binding to the real network interface.
Definition virtual.h:154
enum net_l2_flags virtual_l2_flags
This tells what L2 features does virtual support.
Definition virtual.h:173
struct net_if * iface
Other network interface this virtual network interface is attached to.
Definition virtual.h:168
char name[VIRTUAL_MAX_NAME_LEN]
User friendly name of this L2 layer.
Definition virtual.h:182
bool is_init
Is this context already initialized.
Definition virtual.h:176
struct net_linkaddr lladdr
Link address for this network interface.
Definition virtual.h:179
Virtual L2 API operations.
Definition virtual.h:107
int(* stop)(const struct device *dev)
Stop the device.
Definition virtual.h:121
int(* get_config)(struct net_if *iface, enum virtual_interface_config_type type, struct virtual_interface_config *config)
Get specific L2 configuration.
Definition virtual.h:143
int(* attach)(struct net_if *virtual_iface, struct net_if *iface)
Pass the attachment information to virtual interface.
Definition virtual.h:135
struct net_if_driver_api iface_api
The net_if_driver_api must be placed in first position in this struct so that we are compatible with ...
Definition virtual.h:112
int(* start)(const struct device *dev)
Start the device.
Definition virtual.h:118
enum virtual_interface_caps(* get_capabilities)(struct net_if *iface)
Get the virtual interface capabilities.
Definition virtual.h:115
enum net_verdict(* recv)(struct net_if *iface, struct net_pkt *pkt)
Receive a network packet.
Definition virtual.h:132
int(* set_config)(struct net_if *iface, enum virtual_interface_config_type type, const struct virtual_interface_config *config)
Set specific L2 configuration.
Definition virtual.h:138
int(* send)(struct net_if *iface, struct net_pkt *pkt)
Send a network packet.
Definition virtual.h:124
Misc utilities.