FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
fib_types.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vnet/ip/ip.h>
17 
18 #include <vnet/fib/fib_types.h>
19 #include <vnet/fib/fib_internal.h>
20 #include <vnet/mpls/mpls.h>
21 
22 /*
23  * arrays of protocol and link names
24  */
25 static const char* fib_protocol_names[] = FIB_PROTOCOLS;
26 static const char* vnet_link_names[] = VNET_LINKS;
27 static const char* fib_forw_chain_names[] = FIB_FORW_CHAINS;
28 
29 u8 *
30 format_fib_protocol (u8 * s, va_list * ap)
31 {
32  fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
33 
34  return (format (s, "%s", fib_protocol_names[proto]));
35 }
36 
37 u8 *
38 format_vnet_link (u8 * s, va_list * ap)
39 {
40  vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
41 
42  return (format (s, "%s", vnet_link_names[link]));
43 }
44 
45 u8 *
46 format_fib_forw_chain_type (u8 * s, va_list * args)
47 {
48  fib_forward_chain_type_t fct = va_arg(*args, int);
49 
50  return (format (s, "%s", fib_forw_chain_names[fct]));
51 }
52 
53 void
54 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
55  fib_prefix_t *pfx)
56 {
58 
59  pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
62  pfx->fp_len = ((ip46_address_is_ip4(addr) ?
63  32 : 128));
64  pfx->fp_addr = *addr;
65 }
66 
67 void
69  mpls_eos_bit_t eos,
70  fib_prefix_t *pfx)
71 {
73  pfx->fp_len = 21;
74  pfx->fp_label = label;
75  pfx->fp_eos = eos;
76 }
77 
78 int
80  const fib_prefix_t *p2)
81 {
82  int res;
83 
84  res = (p1->fp_proto - p2->fp_proto);
85 
86  if (0 == res)
87  {
88  switch (p1->fp_proto)
89  {
90  case FIB_PROTOCOL_IP4:
91  case FIB_PROTOCOL_IP6:
92  res = (p1->fp_len - p2->fp_len);
93 
94  if (0 == res)
95  {
96  res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
97  }
98  break;
99  case FIB_PROTOCOL_MPLS:
100  res = (p1->fp_label - p2->fp_label);
101 
102  if (0 == res)
103  {
104  res = (p1->fp_eos - p2->fp_eos);
105  }
106  break;
107  }
108  }
109 
110  return (res);
111 }
112 
113 int
115  const fib_prefix_t *p2)
116 {
117  switch (p1->fp_proto)
118  {
119  case FIB_PROTOCOL_IP4:
121  &p1->fp_addr.ip4,
122  &p2->fp_addr.ip4,
123  p1->fp_len));
124  case FIB_PROTOCOL_IP6:
126  &p1->fp_addr.ip6,
127  &p2->fp_addr.ip6,
128  p1->fp_len));
129  case FIB_PROTOCOL_MPLS:
130  break;
131  }
132  return (0);
133 }
134 
135 int
137 {
138  switch (prefix->fp_proto)
139  {
140  case FIB_PROTOCOL_IP4:
141  return (prefix->fp_len == 32);
142  case FIB_PROTOCOL_IP6:
143  return (prefix->fp_len == 128);
144  case FIB_PROTOCOL_MPLS:
145  return (!0);
146  }
147  return (0);
148 }
149 
150 u8 *
151 format_fib_prefix (u8 * s, va_list * args)
152 {
153  fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
154 
155  /*
156  * protocol specific so it prints ::/0 correctly.
157  */
158  switch (fp->fp_proto)
159  {
160  case FIB_PROTOCOL_IP6:
161  {
162  ip6_address_t p6 = fp->fp_addr.ip6;
163 
165  s = format (s, "%U", format_ip6_address, &p6);
166  break;
167  }
168  case FIB_PROTOCOL_IP4:
169  {
170  ip4_address_t p4 = fp->fp_addr.ip4;
171  p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
172 
173  s = format (s, "%U", format_ip4_address, &p4);
174  break;
175  }
176  case FIB_PROTOCOL_MPLS:
177  s = format (s, "%U:%U",
180  break;
181  }
182  s = format (s, "/%d", fp->fp_len);
183 
184  return (s);
185 }
186 
187 int
189  const fib_route_path_t *rpath2)
190 {
191  int res;
192 
193  res = ip46_address_cmp(&rpath1->frp_addr,
194  &rpath2->frp_addr);
195 
196  if (0 != res) return (res);
197 
198  res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
199 
200  if (0 != res) return (res);
201 
202  if (ip46_address_is_zero(&rpath1->frp_addr))
203  {
204  res = rpath1->frp_fib_index - rpath2->frp_fib_index;
205  }
206 
207  return (res);
208 }
209 
212 {
213  switch (fib_proto)
214  {
215  case FIB_PROTOCOL_IP6:
216  return (DPO_PROTO_IP6);
217  case FIB_PROTOCOL_IP4:
218  return (DPO_PROTO_IP4);
219  case FIB_PROTOCOL_MPLS:
220  return (DPO_PROTO_MPLS);
221  }
222  ASSERT(0);
223  return (0);
224 }
225 
228 {
229  switch (dpo_proto)
230  {
231  case DPO_PROTO_IP6:
232  return (FIB_PROTOCOL_IP6);
233  case DPO_PROTO_IP4:
234  return (FIB_PROTOCOL_IP4);
235  case DPO_PROTO_MPLS:
236  return (FIB_PROTOCOL_MPLS);
237  default:
238  break;
239  }
240  ASSERT(0);
241  return (0);
242 }
243 
246 {
247  switch (proto)
248  {
249  case FIB_PROTOCOL_IP4:
250  return (VNET_LINK_IP4);
251  case FIB_PROTOCOL_IP6:
252  return (VNET_LINK_IP6);
253  case FIB_PROTOCOL_MPLS:
254  return (VNET_LINK_MPLS);
255  }
256  ASSERT(0);
257  return (0);
258 }
259 
262 {
263  switch (proto)
264  {
265  case DPO_PROTO_IP4:
267  case DPO_PROTO_IP6:
269  case DPO_PROTO_MPLS:
271  case DPO_PROTO_ETHERNET:
273  case DPO_PROTO_NSH:
274  return (FIB_FORW_CHAIN_TYPE_NSH);
275  case DPO_PROTO_BIER:
276  return (FIB_FORW_CHAIN_TYPE_BIER);
277  }
278  ASSERT(0);
280 }
281 
284 {
285  switch (fct)
286  {
289  return (VNET_LINK_IP4);
292  return (VNET_LINK_IP6);
294  return (VNET_LINK_ETHERNET);
296  return (VNET_LINK_NSH);
299  /*
300  * insufficient information to to convert
301  */
302  ASSERT(0);
303  break;
305  return (VNET_LINK_MPLS);
306  }
307  return (VNET_LINK_IP4);
308 }
309 
312 {
313  switch (fct)
314  {
317  return (DPO_PROTO_IP4);
320  return (DPO_PROTO_IP6);
322  return (DPO_PROTO_ETHERNET);
324  return (DPO_PROTO_NSH);
326  return (DPO_PROTO_BIER);
329  return (DPO_PROTO_MPLS);
330  }
331  return (DPO_PROTO_IP4);
332 }
333 
334 uword
335 unformat_fib_route_path (unformat_input_t * input, va_list * args)
336 {
337  fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
338  u32 *payload_proto = va_arg (*args, u32*);
339  u32 weight, preference, udp_encap_id;
340  mpls_label_t out_label;
341  vnet_main_t *vnm;
342 
343  vnm = vnet_get_main ();
344  memset(rpath, 0, sizeof(*rpath));
345  rpath->frp_weight = 1;
346  rpath->frp_sw_if_index = ~0;
347 
349  {
350  if (unformat (input, "%U %U",
352  &rpath->frp_addr.ip4,
354  &rpath->frp_sw_if_index))
355  {
356  rpath->frp_proto = DPO_PROTO_IP4;
357  }
358  else if (unformat (input, "%U %U",
360  &rpath->frp_addr.ip6,
362  &rpath->frp_sw_if_index))
363  {
364  rpath->frp_proto = DPO_PROTO_IP6;
365  }
366  else if (unformat (input, "weight %u", &weight))
367  {
368  rpath->frp_weight = weight;
369  }
370  else if (unformat (input, "preference %u", &preference))
371  {
372  rpath->frp_preference = preference;
373  }
374  else if (unformat (input, "%U next-hop-table %d",
376  &rpath->frp_addr.ip4,
377  &rpath->frp_fib_index))
378  {
379  rpath->frp_sw_if_index = ~0;
380  rpath->frp_proto = DPO_PROTO_IP4;
381  }
382  else if (unformat (input, "%U next-hop-table %d",
384  &rpath->frp_addr.ip6,
385  &rpath->frp_fib_index))
386  {
387  rpath->frp_sw_if_index = ~0;
388  rpath->frp_proto = DPO_PROTO_IP6;
389  }
390  else if (unformat (input, "%U",
392  &rpath->frp_addr.ip4))
393  {
394  /*
395  * the recursive next-hops are by default in the default table
396  */
397  rpath->frp_fib_index = 0;
398  rpath->frp_sw_if_index = ~0;
399  rpath->frp_proto = DPO_PROTO_IP4;
400  }
401  else if (unformat (input, "%U",
403  &rpath->frp_addr.ip6))
404  {
405  rpath->frp_fib_index = 0;
406  rpath->frp_sw_if_index = ~0;
407  rpath->frp_proto = DPO_PROTO_IP6;
408  }
409  else if (unformat (input, "udp-encap %d", &udp_encap_id))
410  {
411  rpath->frp_udp_encap_id = udp_encap_id;
413  rpath->frp_proto = *payload_proto;
414  }
415  else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
416  {
417  rpath->frp_proto = *payload_proto;
418  rpath->frp_sw_if_index = ~0;
419  rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
420  }
421  else if (unformat (input, "resolve-via-host"))
422  {
424  }
425  else if (unformat (input, "resolve-via-attached"))
426  {
428  }
429  else if (unformat (input,
430  "ip4-lookup-in-table %d",
431  &rpath->frp_fib_index))
432  {
433  rpath->frp_proto = DPO_PROTO_IP4;
434  *payload_proto = DPO_PROTO_IP4;
435  }
436  else if (unformat (input,
437  "ip6-lookup-in-table %d",
438  &rpath->frp_fib_index))
439  {
440  rpath->frp_proto = DPO_PROTO_IP6;
441  *payload_proto = DPO_PROTO_IP6;
442  }
443  else if (unformat (input,
444  "mpls-lookup-in-table %d",
445  &rpath->frp_fib_index))
446  {
447  rpath->frp_proto = DPO_PROTO_MPLS;
448  *payload_proto = DPO_PROTO_MPLS;
449  }
450  else if (unformat (input,
451  "l2-input-on %U",
453  &rpath->frp_sw_if_index))
454  {
455  rpath->frp_proto = DPO_PROTO_ETHERNET;
456  *payload_proto = DPO_PROTO_ETHERNET;
457  }
458  else if (unformat (input, "via-label %U",
460  &rpath->frp_local_label))
461  {
462  rpath->frp_eos = MPLS_NON_EOS;
463  rpath->frp_proto = DPO_PROTO_MPLS;
464  rpath->frp_sw_if_index = ~0;
465  }
466  else if (unformat (input, "rx-ip4 %U",
468  &rpath->frp_sw_if_index))
469  {
470  rpath->frp_proto = DPO_PROTO_IP4;
472  }
473  else if (unformat (input, "out-labels"))
474  {
475  while (unformat (input, "%U",
476  unformat_mpls_unicast_label, &out_label))
477  {
478  vec_add1(rpath->frp_label_stack, out_label);
479  }
480  }
481  else if (unformat (input, "%U",
483  &rpath->frp_sw_if_index))
484  {
485  rpath->frp_proto = *payload_proto;
486  }
487  else
488  {
489  return (0);
490  }
491  }
492 
493  return (1);
494 }
static const char * vnet_link_names[]
Definition: fib_types.c:26
static void ip6_address_mask(ip6_address_t *a, ip6_address_t *mask)
Definition: ip6_packet.h:235
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:181
Contribute an object that is to be used to forward BIER packets.
Definition: fib_types.h:101
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:116
ip46_address_t frp_addr
The next-hop address.
Definition: fib_types.h:393
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:92
static const char * fib_protocol_names[]
Definition: fib_types.c:25
mpls_eos_bit_t frp_eos
EOS bit for the resolving label.
Definition: fib_types.h:404
int fib_route_path_cmp(const fib_route_path_t *rpath1, const fib_route_path_t *rpath2)
Definition: fib_types.c:188
A representation of a path as described by a route producer.
Definition: fib_types.h:377
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
uword unformat_fib_route_path(unformat_input_t *input, va_list *args)
Unformat a fib_route_path_t from CLI input.
Definition: fib_types.c:335
u32 mpls_label_t
A label value only, i.e.
Definition: packet.h:24
dpo_proto_t fib_forw_chain_type_to_dpo_proto(fib_forward_chain_type_t fct)
Convert from a chain type to the DPO proto it will install.
Definition: fib_types.c:311
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
#define VNET_LINKS
Definition: interface.h:261
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:88
static uword ip4_destination_matches_route(const ip4_main_t *im, const ip4_address_t *key, const ip4_address_t *dest, uword dest_length)
Definition: ip4.h:174
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
dpo_proto_t frp_proto
The protocol of the address below.
Definition: fib_types.h:382
unformat_function_t unformat_vnet_sw_interface
A path that result in received traffic being recieved/recirculated so that it appears to have arrived...
Definition: fib_types.h:318
#define ip46_address_cmp(ip46_1, ip46_2)
Definition: ip6_packet.h:80
int fib_prefix_is_host(const fib_prefix_t *prefix)
Return true is the prefix is a host prefix.
Definition: fib_types.c:136
u8 * format_fib_protocol(u8 *s, va_list *ap)
Definition: fib_types.c:30
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
unformat_function_t unformat_mpls_unicast_label
Definition: mpls.h:79
format_function_t format_ip4_address
Definition: format.h:79
unformat_function_t unformat_ip4_address
Definition: format.h:76
u32 frp_sw_if_index
The interface.
Definition: fib_types.h:412
mpls_label_t * frp_label_stack
The outgoing MPLS label Stack.
Definition: fib_types.h:432
static uword ip6_destination_matches_route(const ip6_main_t *im, const ip6_address_t *key, const ip6_address_t *dest, uword dest_length)
Definition: ip6.h:239
Recursion constraint of via a host prefix.
Definition: fib_types.h:293
u8 * format_fib_prefix(u8 *s, va_list *args)
Definition: fib_types.c:151
Aggregrate type for a prefix.
Definition: fib_types.h:172
int fib_prefix_is_cover(const fib_prefix_t *p1, const fib_prefix_t *p2)
Compare two prefixes for covering relationship.
Definition: fib_types.c:114
A path via a UDP encap object.
Definition: fib_types.h:330
Contribute an object that is to be used to forward Ethernet packets.
Definition: fib_types.h:120
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
fib_protocol_t dpo_proto_to_fib(dpo_proto_t dpo_proto)
Definition: fib_types.c:227
u16 fp_len
The mask length.
Definition: fib_types.h:176
Contribute an object that is to be used to forward end-of-stack MPLS packets.
Definition: fib_types.h:108
format_function_t format_mpls_eos_bit
Definition: mpls.h:67
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:195
struct _unformat_input_t unformat_input_t
static const char * fib_forw_chain_names[]
Definition: fib_types.c:27
Recursion constraint of via an attahced prefix.
Definition: fib_types.h:297
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
unformat_function_t unformat_ip6_address
Definition: format.h:94
ip6_address_t fib_masks[129]
Definition: ip6.h:170
vnet_link_t fib_forw_chain_type_to_link_type(fib_forward_chain_type_t fct)
Convert from a chain type to the adjacencies link type.
Definition: fib_types.c:283
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
format_function_t format_ip6_address
Definition: format.h:95
Contribute an object that is to be used to forward NSH packets.
Definition: fib_types.h:126
void fib_prefix_from_mpls_label(mpls_label_t label, mpls_eos_bit_t eos, fib_prefix_t *pfx)
Big train switch; FIB debugs on or off.
Definition: fib_types.c:68
void fib_prefix_from_ip46_addr(const ip46_address_t *addr, fib_prefix_t *pfx)
Host prefix from ip.
Definition: fib_types.c:54
mpls_label_t fp_label
Definition: fib_types.h:198
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:3009
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
u8 frp_preference
A path preference.
Definition: fib_types.h:465
enum fib_forward_chain_type_t_ fib_forward_chain_type_t
FIB output chain type.
fib_route_path_flags_t frp_flags
flags on the path
Definition: fib_types.h:469
u8 * format_fib_forw_chain_type(u8 *s, va_list *args)
Definition: fib_types.c:46
#define FIB_FORW_CHAINS
Definition: fib_types.h:129
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:211
A path that resolves via another table.
Definition: fib_types.h:346
u64 uword
Definition: types.h:112
format_function_t format_mpls_unicast_label
Definition: mpls.h:69
mpls_label_t frp_local_label
The MPLS local Label to reursively resolve through.
Definition: fib_types.h:400
int fib_prefix_cmp(const fib_prefix_t *p1, const fib_prefix_t *p2)
Compare two prefixes for equality.
Definition: fib_types.c:79
unsigned char u8
Definition: types.h:56
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1181
vnet_link_t fib_proto_to_link(fib_protocol_t proto)
Convert from a protocol to a link type.
Definition: fib_types.c:245
u8 * format_vnet_link(u8 *s, va_list *ap)
Definition: fib_types.c:38
Contribute an object that is to be used to forward non-end-of-stack MPLS packets. ...
Definition: fib_types.h:97
vhost_vring_addr_t addr
Definition: vhost-user.h:83
u8 frp_weight
[un]equal cost path weight
Definition: fib_types.h:459
u32 frp_udp_encap_id
UDP encap ID.
Definition: fib_types.h:449
#define ip46_address_is_zero(ip46)
Definition: ip6_packet.h:81
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:112
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:423
fib_forward_chain_type_t fib_forw_chain_type_from_dpo_proto(dpo_proto_t proto)
Convert from a payload-protocol to a chain type.
Definition: fib_types.c:261
#define FIB_PROTOCOLS
Definition: fib_types.h:41
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
mpls_eos_bit_t fp_eos
Definition: fib_types.h:199
u32 fib_masks[33]
Definition: ip4.h:108
enum mpls_eos_bit_t_ mpls_eos_bit_t