]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.h
Zebra: Data structures for RMAC processing in FPM
[mirror_frr.git] / lib / if.h
1 /* Interface related header.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _ZEBRA_IF_H
22 #define _ZEBRA_IF_H
23
24 #include "zebra.h"
25 #include "linklist.h"
26 #include "memory.h"
27 #include "qobj.h"
28 #include "hook.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 DECLARE_MTYPE(IF)
35 DECLARE_MTYPE(CONNECTED_LABEL)
36
37 /* Interface link-layer type, if known. Derived from:
38 *
39 * net/if_arp.h on various platforms - Linux especially.
40 * http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
41 *
42 * Some of the more obviously defunct technologies left out.
43 */
44 enum zebra_link_type {
45 ZEBRA_LLT_UNKNOWN = 0,
46 ZEBRA_LLT_ETHER,
47 ZEBRA_LLT_EETHER,
48 ZEBRA_LLT_AX25,
49 ZEBRA_LLT_PRONET,
50 ZEBRA_LLT_IEEE802,
51 ZEBRA_LLT_ARCNET,
52 ZEBRA_LLT_APPLETLK,
53 ZEBRA_LLT_DLCI,
54 ZEBRA_LLT_ATM,
55 ZEBRA_LLT_METRICOM,
56 ZEBRA_LLT_IEEE1394,
57 ZEBRA_LLT_EUI64,
58 ZEBRA_LLT_INFINIBAND,
59 ZEBRA_LLT_SLIP,
60 ZEBRA_LLT_CSLIP,
61 ZEBRA_LLT_SLIP6,
62 ZEBRA_LLT_CSLIP6,
63 ZEBRA_LLT_RSRVD,
64 ZEBRA_LLT_ADAPT,
65 ZEBRA_LLT_ROSE,
66 ZEBRA_LLT_X25,
67 ZEBRA_LLT_PPP,
68 ZEBRA_LLT_CHDLC,
69 ZEBRA_LLT_LAPB,
70 ZEBRA_LLT_RAWHDLC,
71 ZEBRA_LLT_IPIP,
72 ZEBRA_LLT_IPIP6,
73 ZEBRA_LLT_FRAD,
74 ZEBRA_LLT_SKIP,
75 ZEBRA_LLT_LOOPBACK,
76 ZEBRA_LLT_LOCALTLK,
77 ZEBRA_LLT_FDDI,
78 ZEBRA_LLT_SIT,
79 ZEBRA_LLT_IPDDP,
80 ZEBRA_LLT_IPGRE,
81 ZEBRA_LLT_IP6GRE,
82 ZEBRA_LLT_PIMREG,
83 ZEBRA_LLT_HIPPI,
84 ZEBRA_LLT_ECONET,
85 ZEBRA_LLT_IRDA,
86 ZEBRA_LLT_FCPP,
87 ZEBRA_LLT_FCAL,
88 ZEBRA_LLT_FCPL,
89 ZEBRA_LLT_FCFABRIC,
90 ZEBRA_LLT_IEEE802_TR,
91 ZEBRA_LLT_IEEE80211,
92 ZEBRA_LLT_IEEE80211_RADIOTAP,
93 ZEBRA_LLT_IEEE802154,
94 ZEBRA_LLT_IEEE802154_PHY,
95 };
96
97 /*
98 Interface name length.
99
100 Linux define value in /usr/include/linux/if.h.
101 #define IFNAMSIZ 16
102
103 FreeBSD define value in /usr/include/net/if.h.
104 #define IFNAMSIZ 16
105 */
106
107 #define INTERFACE_NAMSIZ 20
108 #define INTERFACE_HWADDR_MAX 20
109
110 typedef signed int ifindex_t;
111
112 #ifdef HAVE_PROC_NET_DEV
113 struct if_stats {
114 unsigned long rx_packets; /* total packets received */
115 unsigned long tx_packets; /* total packets transmitted */
116 unsigned long rx_bytes; /* total bytes received */
117 unsigned long tx_bytes; /* total bytes transmitted */
118 unsigned long rx_errors; /* bad packets received */
119 unsigned long tx_errors; /* packet transmit problems */
120 unsigned long rx_dropped; /* no space in linux buffers */
121 unsigned long tx_dropped; /* no space available in linux */
122 unsigned long rx_multicast; /* multicast packets received */
123 unsigned long rx_compressed;
124 unsigned long tx_compressed;
125 unsigned long collisions;
126
127 /* detailed rx_errors: */
128 unsigned long rx_length_errors;
129 unsigned long rx_over_errors; /* receiver ring buff overflow */
130 unsigned long rx_crc_errors; /* recved pkt with crc error */
131 unsigned long rx_frame_errors; /* recv'd frame alignment error */
132 unsigned long rx_fifo_errors; /* recv'r fifo overrun */
133 unsigned long rx_missed_errors; /* receiver missed packet */
134 /* detailed tx_errors */
135 unsigned long tx_aborted_errors;
136 unsigned long tx_carrier_errors;
137 unsigned long tx_fifo_errors;
138 unsigned long tx_heartbeat_errors;
139 unsigned long tx_window_errors;
140 };
141 #endif /* HAVE_PROC_NET_DEV */
142
143 /* Here are "non-official" architectural constants. */
144 #define TE_EXT_MASK 0x0FFFFFFF
145 #define TE_EXT_ANORMAL 0x80000000
146 #define LOSS_PRECISION 0.000003
147 #define TE_KILO_BIT 1000
148 #define TE_BYTE 8
149 #define DEFAULT_BANDWIDTH 10000
150 #define MAX_CLASS_TYPE 8
151 #define MAX_PKT_LOSS 50.331642
152
153 /*
154 * Link Parameters Status:
155 * equal to 0: unset
156 * different from 0: set
157 */
158 #define LP_UNSET 0x0000
159 #define LP_TE_METRIC 0x0001
160 #define LP_MAX_BW 0x0002
161 #define LP_MAX_RSV_BW 0x0004
162 #define LP_UNRSV_BW 0x0008
163 #define LP_ADM_GRP 0x0010
164 #define LP_RMT_AS 0x0020
165 #define LP_DELAY 0x0040
166 #define LP_MM_DELAY 0x0080
167 #define LP_DELAY_VAR 0x0100
168 #define LP_PKT_LOSS 0x0200
169 #define LP_RES_BW 0x0400
170 #define LP_AVA_BW 0x0800
171 #define LP_USE_BW 0x1000
172
173 #define IS_PARAM_UNSET(lp, st) !(lp->lp_status & st)
174 #define IS_PARAM_SET(lp, st) (lp->lp_status & st)
175 #define IS_LINK_PARAMS_SET(lp) (lp->lp_status != LP_UNSET)
176
177 #define SET_PARAM(lp, st) (lp->lp_status) |= (st)
178 #define UNSET_PARAM(lp, st) (lp->lp_status) &= ~(st)
179 #define RESET_LINK_PARAM(lp) (lp->lp_status = LP_UNSET)
180
181 /* Link Parameters for Traffic Engineering */
182 struct if_link_params {
183 uint32_t lp_status; /* Status of Link Parameters: */
184 uint32_t te_metric; /* Traffic Engineering metric */
185 float default_bw;
186 float max_bw; /* Maximum Bandwidth */
187 float max_rsv_bw; /* Maximum Reservable Bandwidth */
188 float unrsv_bw[MAX_CLASS_TYPE]; /* Unreserved Bandwidth per Class Type
189 (8) */
190 uint32_t admin_grp; /* Administrative group */
191 uint32_t rmt_as; /* Remote AS number */
192 struct in_addr rmt_ip; /* Remote IP address */
193 uint32_t av_delay; /* Link Average Delay */
194 uint32_t min_delay; /* Link Min Delay */
195 uint32_t max_delay; /* Link Max Delay */
196 uint32_t delay_var; /* Link Delay Variation */
197 float pkt_loss; /* Link Packet Loss */
198 float res_bw; /* Residual Bandwidth */
199 float ava_bw; /* Available Bandwidth */
200 float use_bw; /* Utilized Bandwidth */
201 };
202
203 #define INTERFACE_LINK_PARAMS_SIZE sizeof(struct if_link_params)
204 #define HAS_LINK_PARAMS(ifp) ((ifp)->link_params != NULL)
205
206 /* Interface structure */
207 struct interface {
208 RB_ENTRY(interface) name_entry, index_entry;
209
210 /* Interface name. This should probably never be changed after the
211 interface is created, because the configuration info for this
212 interface
213 is associated with this structure. For that reason, the interface
214 should also never be deleted (to avoid losing configuration info).
215 To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
216 interface does not exist in the kernel.
217 */
218 char name[INTERFACE_NAMSIZ];
219
220 /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
221 deleted interfaces).
222 WARNING: the ifindex needs to be changed using the if_set_index()
223 function. Failure to respect this will cause corruption in the data
224 structure used to store the interfaces and if_lookup_by_index() will
225 not work as expected.
226 */
227 ifindex_t ifindex;
228 /*
229 * ifindex of parent interface, if any
230 */
231 ifindex_t link_ifindex;
232 #define IFINDEX_INTERNAL 0
233
234 /* Zebra internal interface status */
235 uint8_t status;
236 #define ZEBRA_INTERFACE_ACTIVE (1 << 0)
237 #define ZEBRA_INTERFACE_SUB (1 << 1)
238 #define ZEBRA_INTERFACE_LINKDETECTION (1 << 2)
239 #define ZEBRA_INTERFACE_VRF_LOOPBACK (1 << 3)
240
241 /* Interface flags. */
242 uint64_t flags;
243
244 /* Interface metric */
245 uint32_t metric;
246
247 /* Interface Speed in Mb/s */
248 uint32_t speed;
249
250 /* Interface MTU. */
251 unsigned int mtu; /* IPv4 MTU */
252 unsigned int
253 mtu6; /* IPv6 MTU - probably, but not neccessarily same as mtu
254 */
255
256 /* Link-layer information and hardware address */
257 enum zebra_link_type ll_type;
258 uint8_t hw_addr[INTERFACE_HWADDR_MAX];
259 int hw_addr_len;
260
261 /* interface bandwidth, kbits */
262 unsigned int bandwidth;
263
264 /* Link parameters for Traffic Engineering */
265 struct if_link_params *link_params;
266
267 /* description of the interface. */
268 char *desc;
269
270 /* Distribute list. */
271 void *distribute_in;
272 void *distribute_out;
273
274 /* Connected address list. */
275 struct list *connected;
276
277 /* Neighbor connected address list. */
278 struct list *nbr_connected;
279
280 /* Daemon specific interface data pointer. */
281 void *info;
282
283 char ptm_enable; /* Should we look at ptm_status ? */
284 char ptm_status;
285
286 /* Statistics fileds. */
287 #ifdef HAVE_PROC_NET_DEV
288 struct if_stats stats;
289 #endif /* HAVE_PROC_NET_DEV */
290 #ifdef HAVE_NET_RT_IFLIST
291 struct if_data stats;
292 #endif /* HAVE_NET_RT_IFLIST */
293
294 struct route_node *node;
295 vrf_id_t vrf_id;
296
297 QOBJ_FIELDS
298 };
299
300 RB_HEAD(if_name_head, interface);
301 RB_PROTOTYPE(if_name_head, interface, name_entry, if_cmp_func)
302 RB_HEAD(if_index_head, interface);
303 RB_PROTOTYPE(if_index_head, interface, index_entry, if_cmp_func)
304 DECLARE_QOBJ_TYPE(interface)
305
306 #define IFNAME_RB_INSERT(vrf, ifp) \
307 if (RB_INSERT(if_name_head, &vrf->ifaces_by_name, (ifp))) \
308 flog_err(EC_LIB_INTERFACE, \
309 "%s(%s): corruption detected -- interface with this " \
310 "name exists already in VRF %u!", \
311 __func__, (ifp)->name, (ifp)->vrf_id);
312
313 #define IFNAME_RB_REMOVE(vrf, ifp) \
314 if (RB_REMOVE(if_name_head, &vrf->ifaces_by_name, (ifp)) == NULL) \
315 flog_err(EC_LIB_INTERFACE, \
316 "%s(%s): corruption detected -- interface with this " \
317 "name doesn't exist in VRF %u!", \
318 __func__, (ifp)->name, (ifp)->vrf_id);
319
320 #define IFINDEX_RB_INSERT(vrf, ifp) \
321 if (RB_INSERT(if_index_head, &vrf->ifaces_by_index, (ifp))) \
322 flog_err(EC_LIB_INTERFACE, \
323 "%s(%u): corruption detected -- interface with this " \
324 "ifindex exists already in VRF %u!", \
325 __func__, (ifp)->ifindex, (ifp)->vrf_id);
326
327 #define IFINDEX_RB_REMOVE(vrf, ifp) \
328 if (RB_REMOVE(if_index_head, &vrf->ifaces_by_index, (ifp)) == NULL) \
329 flog_err(EC_LIB_INTERFACE, \
330 "%s(%u): corruption detected -- interface with this " \
331 "ifindex doesn't exist in VRF %u!", \
332 __func__, (ifp)->ifindex, (ifp)->vrf_id);
333
334 #define FOR_ALL_INTERFACES(vrf, ifp) \
335 if (vrf) \
336 RB_FOREACH (ifp, if_name_head, &vrf->ifaces_by_name)
337
338 #define FOR_ALL_INTERFACES_ADDRESSES(ifp, connected, node) \
339 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected))
340
341 /* called from the library code whenever interfaces are created/deleted
342 * note: interfaces may not be fully realized at that point; also they
343 * may not exist in the system (ifindex = IFINDEX_INTERNAL)
344 *
345 * priority values are important here, daemons should be at 0 while modules
346 * can use 1000+ so they run after the daemon has initialised daemon-specific
347 * interface data
348 */
349 DECLARE_HOOK(if_add, (struct interface * ifp), (ifp))
350 DECLARE_KOOH(if_del, (struct interface * ifp), (ifp))
351
352 #define METRIC_MAX (~0)
353
354 /* Connected address structure. */
355 struct connected {
356 /* Attached interface. */
357 struct interface *ifp;
358
359 /* Flags for configuration. */
360 uint8_t conf;
361 #define ZEBRA_IFC_REAL (1 << 0)
362 #define ZEBRA_IFC_CONFIGURED (1 << 1)
363 #define ZEBRA_IFC_QUEUED (1 << 2)
364 /*
365 The ZEBRA_IFC_REAL flag should be set if and only if this address
366 exists in the kernel and is actually usable. (A case where it exists
367 but
368 is not yet usable would be IPv6 with DAD)
369 The ZEBRA_IFC_CONFIGURED flag should be set if and only if this
370 address
371 was configured by the user from inside quagga.
372 The ZEBRA_IFC_QUEUED flag should be set if and only if the address
373 exists
374 in the kernel. It may and should be set although the address might
375 not be
376 usable yet. (compare with ZEBRA_IFC_REAL)
377 */
378
379 /* Flags for connected address. */
380 uint8_t flags;
381 #define ZEBRA_IFA_SECONDARY (1 << 0)
382 #define ZEBRA_IFA_PEER (1 << 1)
383 #define ZEBRA_IFA_UNNUMBERED (1 << 2)
384 /* N.B. the ZEBRA_IFA_PEER flag should be set if and only if
385 a peer address has been configured. If this flag is set,
386 the destination field must contain the peer address.
387 Otherwise, if this flag is not set, the destination address
388 will either contain a broadcast address or be NULL.
389 */
390
391 /* Address of connected network. */
392 struct prefix *address;
393
394 /* Peer or Broadcast address, depending on whether ZEBRA_IFA_PEER is
395 set.
396 Note: destination may be NULL if ZEBRA_IFA_PEER is not set. */
397 struct prefix *destination;
398
399 /* Label for Linux 2.2.X and upper. */
400 char *label;
401
402 /*
403 * Used for setting the connected route's cost. If the metric
404 * here is set to METRIC_MAX the connected route falls back to
405 * "struct interface"
406 */
407 uint32_t metric;
408 };
409
410 /* Nbr Connected address structure. */
411 struct nbr_connected {
412 /* Attached interface. */
413 struct interface *ifp;
414
415 /* Address of connected network. */
416 struct prefix *address;
417 };
418
419 /* Does the destination field contain a peer address? */
420 #define CONNECTED_PEER(C) CHECK_FLAG((C)->flags, ZEBRA_IFA_PEER)
421
422 /* Prefix to insert into the RIB */
423 #define CONNECTED_PREFIX(C) \
424 (CONNECTED_PEER(C) ? (C)->destination : (C)->address)
425
426 /* Identifying address. We guess that if there's a peer address, but the
427 local address is in the same prefix, then the local address may be unique. */
428 #define CONNECTED_ID(C) \
429 ((CONNECTED_PEER(C) && !prefix_match((C)->destination, (C)->address)) \
430 ? (C)->destination \
431 : (C)->address)
432
433 /* There are some interface flags which are only supported by some
434 operating system. */
435
436 #ifndef IFF_NOTRAILERS
437 #define IFF_NOTRAILERS 0x0
438 #endif /* IFF_NOTRAILERS */
439 #ifndef IFF_OACTIVE
440 #define IFF_OACTIVE 0x0
441 #endif /* IFF_OACTIVE */
442 #ifndef IFF_SIMPLEX
443 #define IFF_SIMPLEX 0x0
444 #endif /* IFF_SIMPLEX */
445 #ifndef IFF_LINK0
446 #define IFF_LINK0 0x0
447 #endif /* IFF_LINK0 */
448 #ifndef IFF_LINK1
449 #define IFF_LINK1 0x0
450 #endif /* IFF_LINK1 */
451 #ifndef IFF_LINK2
452 #define IFF_LINK2 0x0
453 #endif /* IFF_LINK2 */
454 #ifndef IFF_NOXMIT
455 #define IFF_NOXMIT 0x0
456 #endif /* IFF_NOXMIT */
457 #ifndef IFF_NORTEXCH
458 #define IFF_NORTEXCH 0x0
459 #endif /* IFF_NORTEXCH */
460 #ifndef IFF_IPV4
461 #define IFF_IPV4 0x0
462 #endif /* IFF_IPV4 */
463 #ifndef IFF_IPV6
464 #define IFF_IPV6 0x0
465 #endif /* IFF_IPV6 */
466 #ifndef IFF_VIRTUAL
467 #define IFF_VIRTUAL 0x0
468 #endif /* IFF_VIRTUAL */
469
470 /* Prototypes. */
471 extern int if_cmp_name_func(const char *p1, const char *p2);
472
473 /*
474 * Passing in VRF_UNKNOWN is a valid thing to do, unless we
475 * are creating a new interface.
476 *
477 * This is useful for vrf route-leaking. So more than anything
478 * else think before you use VRF_UNKNOWN
479 */
480 extern void if_update_to_new_vrf(struct interface *, vrf_id_t vrf_id);
481 extern struct interface *if_create(const char *name, vrf_id_t vrf_id);
482 extern struct interface *if_lookup_by_index(ifindex_t, vrf_id_t vrf_id);
483 extern struct interface *if_lookup_exact_address(void *matchaddr, int family,
484 vrf_id_t vrf_id);
485 extern struct connected *if_lookup_address(void *matchaddr, int family,
486 vrf_id_t vrf_id);
487 extern struct interface *if_lookup_prefix(struct prefix *prefix,
488 vrf_id_t vrf_id);
489 size_t if_lookup_by_hwaddr(const uint8_t *hw_addr, size_t addrsz,
490 struct interface ***result, vrf_id_t vrf_id);
491
492 /* These 3 functions are to be used when the ifname argument is terminated
493 by a '\0' character: */
494 extern struct interface *if_lookup_by_name_all_vrf(const char *ifname);
495 extern struct interface *if_lookup_by_name(const char *ifname, vrf_id_t vrf_id);
496 extern struct interface *if_get_by_name(const char *ifname, vrf_id_t vrf_id);
497 extern void if_set_index(struct interface *ifp, ifindex_t ifindex);
498
499 /* Delete the interface, but do not free the structure, and leave it in the
500 interface list. It is often advisable to leave the pseudo interface
501 structure because there may be configuration information attached. */
502 extern void if_delete_retain(struct interface *);
503
504 /* Delete and free the interface structure: calls if_delete_retain and then
505 deletes it from the interface list and frees the structure. */
506 extern void if_delete(struct interface *);
507
508 extern int if_is_up(const struct interface *ifp);
509 extern int if_is_running(const struct interface *ifp);
510 extern int if_is_operative(const struct interface *ifp);
511 extern int if_is_no_ptm_operative(const struct interface *ifp);
512 extern int if_is_loopback(const struct interface *ifp);
513 extern int if_is_vrf(const struct interface *ifp);
514 extern bool if_is_loopback_or_vrf(const struct interface *ifp);
515 extern int if_is_broadcast(const struct interface *ifp);
516 extern int if_is_pointopoint(const struct interface *ifp);
517 extern int if_is_multicast(const struct interface *ifp);
518 struct vrf;
519 extern void if_terminate(struct vrf *vrf);
520 extern void if_dump_all(void);
521 extern const char *if_flag_dump(unsigned long);
522 extern const char *if_link_type_str(enum zebra_link_type);
523
524 /* Please use ifindex2ifname instead of if_indextoname where possible;
525 ifindex2ifname uses internal interface info, whereas if_indextoname must
526 make a system call. */
527 extern const char *ifindex2ifname(ifindex_t, vrf_id_t vrf_id);
528
529 /* Please use ifname2ifindex instead of if_nametoindex where possible;
530 ifname2ifindex uses internal interface info, whereas if_nametoindex must
531 make a system call. */
532 extern ifindex_t ifname2ifindex(const char *ifname, vrf_id_t vrf_id);
533
534 /* Connected address functions. */
535 extern struct connected *connected_new(void);
536 extern void connected_free(struct connected *);
537 extern void connected_add(struct interface *, struct connected *);
538 extern struct connected *
539 connected_add_by_prefix(struct interface *, struct prefix *, struct prefix *);
540 extern struct connected *connected_delete_by_prefix(struct interface *,
541 struct prefix *);
542 extern struct connected *connected_lookup_prefix(struct interface *,
543 struct prefix *);
544 extern struct connected *connected_lookup_prefix_exact(struct interface *,
545 struct prefix *);
546 extern struct nbr_connected *nbr_connected_new(void);
547 extern void nbr_connected_free(struct nbr_connected *);
548 struct nbr_connected *nbr_connected_check(struct interface *, struct prefix *);
549 struct connected *connected_get_linklocal(struct interface *ifp);
550
551 /* link parameters */
552 struct if_link_params *if_link_params_get(struct interface *);
553 void if_link_params_free(struct interface *);
554
555 /* Northbound. */
556 extern void if_cmd_init(void);
557 extern const struct frr_yang_module_info frr_interface_info;
558
559 #ifdef __cplusplus
560 }
561 #endif
562
563 #endif /* _ZEBRA_IF_H */