CNDP 22.08.0
Loading...
Searching...
No Matches
cnet_netif.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2016-2022 Intel Corporation
3 */
4
5#ifndef __CNET_NETIF_H
6#define __CNET_NETIF_H
7
12
13#include <net/if.h>
14#include <cne_atomic.h>
15#include <pktdev.h>
16#include <cne_inet.h> // for inet_addr_mask_cmp, inet_ntop4
17#include <stddef.h> // for NULL
18#include <stdint.h> // for uint64_t, uint8_t, uint16_t, uint32_t, int32_t
19
20#include "cne_common.h" // for __cne_cache_aligned
21#include "cne_log.h" // for CNE_LOG, CNE_LOG_DEBUG, CNE_LOG_WARNING
22#include "cne_lport.h" // for lport_stats
23#include "cne_vec.h" // for vec_at_index, vec_pool_free
24#include "cnet_const.h" // for iofunc_t
25#include "cne_inet.h" // for _in_addr, _in6_addr
26#include "cnet_stk.h" // for stk_entry, per_thread_stk, this_stk
27#include "mempool.h" // for mempool_get, mempool_obj_iter, mempool_put
28#include "pktmbuf.h" // for pktmbuf_t
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34enum { NETIF_IFNAME_TYPE, NETIF_NETDEV_NAME_TYPE };
35
36enum { PRIMARY_IPADDR = 0, DEFAULT_FORWARDING_STATE = 1, NUM_IP_ADDRS = 8 };
37
38/* Network interface flags */
39enum {
40 _IFF_UP = 0x00000001, /* interface link is up */
41 _IFF_BROADCAST = 0x00000002, /* broadcast address valid */
42 _IFF_DEBUG = 0x00000004, /* turn on debugging */
43 _IFF_LOOPBACK = 0x00000008, /* is a loopback net */
44 _IFF_POINTOPOINT = 0x00000010, /* interface is p2p link */
45 _IFF_SMART = 0x00000020, /* interface manages own routes */
46 _IFF_RUNNING = 0x00000040, /* resources allocated */
47 _IFF_NOARP = 0x00000080, /* no address resolution protocol */
48 _IFF_PROMISC = 0x00000100, /* receive all packets */
49 _IFF_ALLMULTI = 0x00000200, /* receive all multicast packets */
50 _IFF_OACTIVE = 0x00000400, /* transmission in progress */
51 _IFF_SIMPLEX = 0x00000800, /* can't hear own transmissions */
52 _IFF_LINK0 = 0x00001000, /* forwarding disabled */
53 _IFF_LINK1 = 0x00002000, /* per link layer defined bit */
54 _IFF_LINK2 = 0x00004000, /* per link layer defined bit */
55 _IFF_MULTICAST = 0x00008000, /* supports multicast */
56 _IFF_NOTRAILERS = 0x00020000, /* avoid use of trailers */
57 _IFF_INET_UP = 0x00040000, /* interface is up for ipv4 */
58 _IFF_INET6_UP = 0x00080000, /* interface is up for ipv6 */
59 _IFF_RARP = 0x00100000, /* RARP enabled on this interface */
60 _IFF_DONT_FORWARD = 0x00200000 /* Not allowed to forward packets */
61};
62
63struct netif;
64struct drv_entry;
65
66/* Structure to contain all of the IPv4 Addresses */
67struct inet4_addr {
68 uint16_t valid;
69 uint16_t prefixlen;
70 struct in_addr ip;
71 struct in_addr netmask;
72 struct in_addr broadcast;
73};
74
75struct netif {
76 int16_t netif_idx;
77 uint16_t lpid;
78 int ifflags;
79 int ifindex;
80 uint16_t ip_ident;
81 uint16_t family;
82 uint16_t mtu;
83 char ifname[IF_NAMESIZE + 1];
84 char netdev_name[IF_NAMESIZE + 1];
85 struct drv_entry *drv;
86 struct rt4_entry *rt_cached;
87 struct inet4_addr ip4_addrs[NUM_IP_ADDRS];
88 struct ether_addr mac;
90
91#define _IFF_UP 0x00000001
92#define _IFF_BROADCAST 0x00000002
93#define _IFF_DEBUG 0x00000004
94#define _IFF_LOOPBACK 0x00000008
95#define _IFF_POINTOPOINT 0x00000010
96#define _IFF_SMART 0x00000020
97#define _IFF_RUNNING 0x00000040
98#define _IFF_NOARP 0x00000080
99#define _IFF_PROMISC 0x00000100
100#define _IFF_ALLMULTI 0x00000200
101#define _IFF_OACTIVE 0x00000400
102#define _IFF_SIMPLEX 0x00000800
103#define _IFF_LINK0 0x00001000
104#define _IFF_LINK1 0x00002000
105#define _IFF_LINK2 0x00004000
106#define _IFF_MULTICAST 0x00008000
107#define _IFF_NOTRAILERS 0x00020000
108#define _IFF_INET_UP 0x00040000
109#define _IFF_INET6_UP 0x00080000
110#define _IFF_RARP 0x00100000
111#define _IFF_DONT_FORWARD 0x00200000
112
113#define _ipv4_broadcast_compare(_i) \
114 do { \
115 if (netif->ip4_addrs[_i].ip.s_addr) { \
116 if (ip->s_addr == netif->ip4_addrs[_i].broadcast.s_addr) \
117 return _i; \
118 } \
119 } while (/*CONSTCOND*/ 0)
120
124static inline int
125cnet_ipv4_broadcast(struct netif *netif, struct in_addr *ip)
126{
127 if (NUM_IP_ADDRS == 4) {
128 _ipv4_broadcast_compare(0);
129 _ipv4_broadcast_compare(1);
130 _ipv4_broadcast_compare(2);
131 _ipv4_broadcast_compare(3);
132 return -1;
133 }
134 for (int i = 0; i < NUM_IP_ADDRS; i++)
135 _ipv4_broadcast_compare(i);
136 return -1;
137}
138
139#define _ipv4_compare(_i) \
140 do { \
141 struct in_addr *ip2, *mask; \
142 ip2 = &netif->ip4_addrs[_i].ip; \
143 mask = &netif->ip4_addrs[_i].netmask; \
144 if (inet_addr_mask_cmp(&addr, ip2, mask)) \
145 return _i; \
146 } while (/*CONSTCOND*/ 0)
147
148static inline int
149cnet_ipv4_compare(struct netif *netif, struct in_addr *ip)
150{
151 struct in_addr addr;
152
153 addr.s_addr = ntohl(ip->s_addr);
154 if (NUM_IP_ADDRS == 4) {
155 _ipv4_compare(0);
156 _ipv4_compare(1);
157 _ipv4_compare(2);
158 _ipv4_compare(3);
159 return -1;
160 }
161 for (int i = 0; i < NUM_IP_ADDRS; i++)
162 _ipv4_compare(i);
163
164 return -1;
165}
166
170static inline struct netif *
172{
173 return vec_at_index(this_cnet->netifs, idx);
174}
175
179static inline struct netif *
180cnet_netif_match_subnet(struct in_addr *ipaddr)
181{
182 struct netif **netif;
183
184 vec_foreach (netif, this_cnet->netifs) {
185 if (cnet_ipv4_compare(*netif, ipaddr) != -1)
186 return *netif;
187 }
188 return NULL;
189}
190
194static inline void
195cnet_netif_free(struct netif *netif)
196{
197 if (netif) {
198 if (netif->netif_idx >= 0 && netif->netif_idx < CNE_MAX_ETHPORTS)
199 vec_at_index(this_cnet->netifs, netif->netif_idx) = NULL;
200 free(netif);
201 }
202}
203
207static inline struct netif *
208cnet_netif_alloc(uint16_t lpid)
209{
210 struct netif *netif = NULL;
211
212 netif = calloc(1, sizeof(struct netif));
213 if (!netif)
214 return NULL;
215
216 netif->netif_idx = -1;
217 netif->ifindex = -1;
218 netif->lpid = lpid;
219 netif->ip_ident = (uint16_t)(cne_rdtsc() & 0xFFFF);
220
221 return netif;
222}
223
227static inline int
228cnet_netif_get_flags(struct netif *netif, uint32_t *flags)
229{
230 if (!flags || !netif)
231 return -1;
232
233 *flags = netif->ifflags;
234
235 return 0;
236}
237
250CNDP_API int cnet_netif_register(uint16_t lpid, char *ifname, char *netdev);
251
260CNDP_API int cnet_netif_attach_ports(struct cnet *cnet);
261
272CNDP_API struct netif *cnet_netif_from_name(const char *name, int typ);
273
284CNDP_API int cnet_netif_set_mtu(struct netif *netif, uint16_t mtu);
285
296CNDP_API int cnet_netif_foreach(int (*func)(struct netif *netif, void *arg), void *arg);
297
308CNDP_API struct inet4_addr *cnet_ipv4_ipaddr_find(struct netif *netif, struct in_addr *ip);
309
320CNDP_API int cnet_ipv4_ipaddr_delete(struct netif *netif, struct in_addr *ip);
321
332CNDP_API int cnet_ipv4_ipaddr_add(struct netif *netif, struct inet4_addr *ip);
333
344CNDP_API int cnet_netif_set_flags(struct netif *netif, uint32_t flags);
345
354CNDP_API struct netif *cnet_netif_find_by_name(char *ifname);
355
364CNDP_API struct netif *cnet_netif_find_by_ifindex(int ifindex);
365
374CNDP_API struct netif *cnet_netif_find_by_netdev(char *netdev_name);
375
384CNDP_API struct netif *cnet_netif_find_by_lport(int lport);
385
394CNDP_API int cnet_is_ifname_valid(char *ifname);
395
404CNDP_API int cnet_is_netdev_valid(char *netdev_name);
405
414CNDP_API int cnet_is_ifindex_valid(int ifindex);
415
416#ifdef __cplusplus
417}
418#endif
419
420#endif /* __CNET_NETIF_H */
#define __cne_cache_aligned
Definition cne_common.h:379
CNDP_API int cnet_netif_register(uint16_t lpid, char *ifname, char *netdev)
Register lport, ifname and netdev to create a netif structure.
CNDP_API struct netif * cnet_netif_find_by_ifindex(int ifindex)
Locate the netif structure by the ifindex value.
#define _IFF_LINK1
Definition cnet_netif.h:104
static struct netif * cnet_netif_from_index(uint8_t idx)
Definition cnet_netif.h:171
#define _IFF_LINK0
Definition cnet_netif.h:103
CNDP_API int cnet_netif_set_flags(struct netif *netif, uint32_t flags)
Add flags or set the flags to a netif structure.
CNDP_API struct netif * cnet_netif_find_by_netdev(char *netdev_name)
Find the netif structure by the netdev name.
static int cnet_ipv4_broadcast(struct netif *netif, struct in_addr *ip)
Definition cnet_netif.h:125
#define _IFF_POINTOPOINT
Definition cnet_netif.h:95
#define _IFF_RUNNING
Definition cnet_netif.h:97
#define _IFF_MULTICAST
Definition cnet_netif.h:106
#define _IFF_ALLMULTI
Definition cnet_netif.h:100
CNDP_API int cnet_netif_set_mtu(struct netif *netif, uint16_t mtu)
Set the MTU for a given netif structure.
CNDP_API int cnet_ipv4_ipaddr_delete(struct netif *netif, struct in_addr *ip)
Delete the given IPv4 address from the given netif structure.
static struct netif * cnet_netif_match_subnet(struct in_addr *ipaddr)
Definition cnet_netif.h:180
CNDP_API int cnet_netif_attach_ports(struct cnet *cnet)
Attach ports to CNET and the netif structures.
#define _IFF_BROADCAST
Definition cnet_netif.h:92
#define _IFF_NOARP
Definition cnet_netif.h:98
#define _IFF_OACTIVE
Definition cnet_netif.h:101
#define _IFF_SIMPLEX
Definition cnet_netif.h:102
#define _IFF_PROMISC
Definition cnet_netif.h:99
static void cnet_netif_free(struct netif *netif)
Definition cnet_netif.h:195
#define _IFF_INET_UP
Definition cnet_netif.h:108
CNDP_API int cnet_ipv4_ipaddr_add(struct netif *netif, struct inet4_addr *ip)
Add a new IPv4 address to the given netif structure.
#define _IFF_INET6_UP
Definition cnet_netif.h:109
CNDP_API int cnet_is_netdev_valid(char *netdev_name)
Is the netdev name a valid netdev.
static struct netif * cnet_netif_alloc(uint16_t lpid)
Definition cnet_netif.h:208
#define _IFF_LOOPBACK
Definition cnet_netif.h:94
#define _IFF_UP
Definition cnet_netif.h:91
#define _IFF_DEBUG
Definition cnet_netif.h:93
CNDP_API struct netif * cnet_netif_find_by_name(char *ifname)
Find the netif structure for the given interface name.
static int cnet_netif_get_flags(struct netif *netif, uint32_t *flags)
Definition cnet_netif.h:228
#define _IFF_LINK2
Definition cnet_netif.h:105
#define _IFF_NOTRAILERS
Definition cnet_netif.h:107
#define _IFF_RARP
Definition cnet_netif.h:110
#define _IFF_DONT_FORWARD
Definition cnet_netif.h:111
CNDP_API int cnet_is_ifindex_valid(int ifindex)
Is the ifindex valid interface index.
CNDP_API struct inet4_addr * cnet_ipv4_ipaddr_find(struct netif *netif, struct in_addr *ip)
Find the given IPv4 address in a given netif structure.
CNDP_API struct netif * cnet_netif_from_name(const char *name, int typ)
Locate the netif for the given interface name.
#define _IFF_SMART
Definition cnet_netif.h:96
CNDP_API int cnet_is_ifname_valid(char *ifname)
Is the ifname a valid interface name.
CNDP_API struct netif * cnet_netif_find_by_lport(int lport)
Find the netif structure by the lport id.
CNDP_API int cnet_netif_foreach(int(*func)(struct netif *netif, void *arg), void *arg)
look over all netif structures calling the specified function