FD.io VPP  v18.04-17-g3a0d853
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;
29 
30 u8 *
31 format_fib_protocol (u8 * s, va_list * ap)
32 {
33  fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
34 
35  return (format (s, "%s", fib_protocol_names[proto]));
36 }
37 
38 u8 *
39 format_vnet_link (u8 * s, va_list * ap)
40 {
41  vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
42 
43  return (format (s, "%s", vnet_link_names[link]));
44 }
45 
46 u8 *
47 format_fib_forw_chain_type (u8 * s, va_list * args)
48 {
49  fib_forward_chain_type_t fct = va_arg(*args, int);
50 
51  return (format (s, "%s", fib_forw_chain_names[fct]));
52 }
53 
54 u8 *
55 format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
56 {
57  fib_mpls_lsp_mode_t mode = va_arg(*ap, int);
58 
59  return (format (s, "%s", fib_mpls_lsp_mode_names[mode]));
60 }
61 
62 u8 *
63 format_fib_mpls_label (u8 *s, va_list *ap)
64 {
65  fib_mpls_label_t *label = va_arg(*ap, fib_mpls_label_t *);
66 
67  s = format(s, "%U %U ttl:%d exp:%d",
69  label->fml_value,
71  label->fml_mode,
72  label->fml_ttl,
73  label->fml_exp);
74 
75  return (s);
76 }
77 
78 void
79 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
80  fib_prefix_t *pfx)
81 {
83 
84  pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
87  pfx->fp_len = ((ip46_address_is_ip4(addr) ?
88  32 : 128));
89  pfx->fp_addr = *addr;
90 }
91 
92 void
94  mpls_eos_bit_t eos,
95  fib_prefix_t *pfx)
96 {
98  pfx->fp_len = 21;
99  pfx->fp_label = label;
100  pfx->fp_eos = eos;
101 }
102 
103 int
105  const fib_prefix_t *p2)
106 {
107  int res;
108 
109  res = (p1->fp_proto - p2->fp_proto);
110 
111  if (0 == res)
112  {
113  switch (p1->fp_proto)
114  {
115  case FIB_PROTOCOL_IP4:
116  case FIB_PROTOCOL_IP6:
117  res = (p1->fp_len - p2->fp_len);
118 
119  if (0 == res)
120  {
121  res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
122  }
123  break;
124  case FIB_PROTOCOL_MPLS:
125  res = (p1->fp_label - p2->fp_label);
126 
127  if (0 == res)
128  {
129  res = (p1->fp_eos - p2->fp_eos);
130  }
131  break;
132  }
133  }
134 
135  return (res);
136 }
137 
138 int
140  const fib_prefix_t *p2)
141 {
142  switch (p1->fp_proto)
143  {
144  case FIB_PROTOCOL_IP4:
146  &p1->fp_addr.ip4,
147  &p2->fp_addr.ip4,
148  p1->fp_len));
149  case FIB_PROTOCOL_IP6:
151  &p1->fp_addr.ip6,
152  &p2->fp_addr.ip6,
153  p1->fp_len));
154  case FIB_PROTOCOL_MPLS:
155  break;
156  }
157  return (0);
158 }
159 
160 int
162 {
163  switch (prefix->fp_proto)
164  {
165  case FIB_PROTOCOL_IP4:
166  return (prefix->fp_len == 32);
167  case FIB_PROTOCOL_IP6:
168  return (prefix->fp_len == 128);
169  case FIB_PROTOCOL_MPLS:
170  return (!0);
171  }
172  return (0);
173 }
174 
175 u8 *
176 format_fib_prefix (u8 * s, va_list * args)
177 {
178  fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
179 
180  /*
181  * protocol specific so it prints ::/0 correctly.
182  */
183  switch (fp->fp_proto)
184  {
185  case FIB_PROTOCOL_IP6:
186  {
187  ip6_address_t p6 = fp->fp_addr.ip6;
188 
190  s = format (s, "%U", format_ip6_address, &p6);
191  break;
192  }
193  case FIB_PROTOCOL_IP4:
194  {
195  ip4_address_t p4 = fp->fp_addr.ip4;
196  p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
197 
198  s = format (s, "%U", format_ip4_address, &p4);
199  break;
200  }
201  case FIB_PROTOCOL_MPLS:
202  s = format (s, "%U:%U",
205  break;
206  }
207  s = format (s, "/%d", fp->fp_len);
208 
209  return (s);
210 }
211 
212 int
214  const fib_route_path_t *rpath2)
215 {
216  int res;
217 
218  res = ip46_address_cmp(&rpath1->frp_addr,
219  &rpath2->frp_addr);
220 
221  if (0 != res) return (res);
222 
223  res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
224 
225  if (0 != res) return (res);
226 
227  if (ip46_address_is_zero(&rpath1->frp_addr))
228  {
229  res = rpath1->frp_fib_index - rpath2->frp_fib_index;
230  }
231 
232  return (res);
233 }
234 
237 {
238  switch (fib_proto)
239  {
240  case FIB_PROTOCOL_IP6:
241  return (DPO_PROTO_IP6);
242  case FIB_PROTOCOL_IP4:
243  return (DPO_PROTO_IP4);
244  case FIB_PROTOCOL_MPLS:
245  return (DPO_PROTO_MPLS);
246  }
247  ASSERT(0);
248  return (0);
249 }
250 
253 {
254  switch (dpo_proto)
255  {
256  case DPO_PROTO_IP6:
257  return (FIB_PROTOCOL_IP6);
258  case DPO_PROTO_IP4:
259  return (FIB_PROTOCOL_IP4);
260  case DPO_PROTO_MPLS:
261  return (FIB_PROTOCOL_MPLS);
262  default:
263  break;
264  }
265  ASSERT(0);
266  return (0);
267 }
268 
271 {
272  switch (proto)
273  {
274  case FIB_PROTOCOL_IP4:
275  return (VNET_LINK_IP4);
276  case FIB_PROTOCOL_IP6:
277  return (VNET_LINK_IP6);
278  case FIB_PROTOCOL_MPLS:
279  return (VNET_LINK_MPLS);
280  }
281  ASSERT(0);
282  return (0);
283 }
284 
287 {
288  switch (proto)
289  {
290  case DPO_PROTO_IP4:
292  case DPO_PROTO_IP6:
294  case DPO_PROTO_MPLS:
296  case DPO_PROTO_ETHERNET:
298  case DPO_PROTO_NSH:
299  return (FIB_FORW_CHAIN_TYPE_NSH);
300  case DPO_PROTO_BIER:
301  return (FIB_FORW_CHAIN_TYPE_BIER);
302  }
303  ASSERT(0);
305 }
306 
309 {
310  switch (fct)
311  {
314  return (VNET_LINK_IP4);
317  return (VNET_LINK_IP6);
319  return (VNET_LINK_ETHERNET);
321  return (VNET_LINK_NSH);
324  /*
325  * insufficient information to to convert
326  */
327  ASSERT(0);
328  break;
330  return (VNET_LINK_MPLS);
331  }
332  return (VNET_LINK_IP4);
333 }
334 
337 {
338  switch (link_type)
339  {
340  case VNET_LINK_IP4:
342  case VNET_LINK_IP6:
344  case VNET_LINK_MPLS:
346  case VNET_LINK_ETHERNET:
348  case VNET_LINK_NSH:
349  return (FIB_FORW_CHAIN_TYPE_NSH);
350  case VNET_LINK_ARP:
351  break;
352  }
353 
354  ASSERT(0);
356 }
357 
360 {
361  switch (fct)
362  {
365  return (DPO_PROTO_IP4);
368  return (DPO_PROTO_IP6);
370  return (DPO_PROTO_ETHERNET);
372  return (DPO_PROTO_NSH);
374  return (DPO_PROTO_BIER);
377  return (DPO_PROTO_MPLS);
378  }
379  return (DPO_PROTO_IP4);
380 }
381 
382 uword
383 unformat_fib_route_path (unformat_input_t * input, va_list * args)
384 {
385  fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
386  u32 *payload_proto = va_arg (*args, u32*);
387  u32 weight, preference, udp_encap_id;
388  mpls_label_t out_label;
389  vnet_main_t *vnm;
390 
391  vnm = vnet_get_main ();
392  memset(rpath, 0, sizeof(*rpath));
393  rpath->frp_weight = 1;
394  rpath->frp_sw_if_index = ~0;
395 
397  {
398  if (unformat (input, "%U %U",
400  &rpath->frp_addr.ip4,
402  &rpath->frp_sw_if_index))
403  {
404  rpath->frp_proto = DPO_PROTO_IP4;
405  }
406  else if (unformat (input, "%U %U",
408  &rpath->frp_addr.ip6,
410  &rpath->frp_sw_if_index))
411  {
412  rpath->frp_proto = DPO_PROTO_IP6;
413  }
414  else if (unformat (input, "weight %u", &weight))
415  {
416  rpath->frp_weight = weight;
417  }
418  else if (unformat (input, "preference %u", &preference))
419  {
420  rpath->frp_preference = preference;
421  }
422  else if (unformat (input, "%U next-hop-table %d",
424  &rpath->frp_addr.ip4,
425  &rpath->frp_fib_index))
426  {
427  rpath->frp_sw_if_index = ~0;
428  rpath->frp_proto = DPO_PROTO_IP4;
429  }
430  else if (unformat (input, "%U next-hop-table %d",
432  &rpath->frp_addr.ip6,
433  &rpath->frp_fib_index))
434  {
435  rpath->frp_sw_if_index = ~0;
436  rpath->frp_proto = DPO_PROTO_IP6;
437  }
438  else if (unformat (input, "%U",
440  &rpath->frp_addr.ip4))
441  {
442  /*
443  * the recursive next-hops are by default in the default table
444  */
445  rpath->frp_fib_index = 0;
446  rpath->frp_sw_if_index = ~0;
447  rpath->frp_proto = DPO_PROTO_IP4;
448  }
449  else if (unformat (input, "%U",
451  &rpath->frp_addr.ip6))
452  {
453  rpath->frp_fib_index = 0;
454  rpath->frp_sw_if_index = ~0;
455  rpath->frp_proto = DPO_PROTO_IP6;
456  }
457  else if (unformat (input, "udp-encap %d", &udp_encap_id))
458  {
459  rpath->frp_udp_encap_id = udp_encap_id;
461  rpath->frp_proto = *payload_proto;
462  }
463  else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
464  {
465  rpath->frp_proto = *payload_proto;
466  rpath->frp_sw_if_index = ~0;
467  rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
468  }
469  else if (unformat (input, "resolve-via-host"))
470  {
472  }
473  else if (unformat (input, "resolve-via-attached"))
474  {
476  }
477  else if (unformat (input,
478  "ip4-lookup-in-table %d",
479  &rpath->frp_fib_index))
480  {
481  rpath->frp_proto = DPO_PROTO_IP4;
482  *payload_proto = DPO_PROTO_IP4;
483  }
484  else if (unformat (input,
485  "ip6-lookup-in-table %d",
486  &rpath->frp_fib_index))
487  {
488  rpath->frp_proto = DPO_PROTO_IP6;
489  *payload_proto = DPO_PROTO_IP6;
490  }
491  else if (unformat (input,
492  "mpls-lookup-in-table %d",
493  &rpath->frp_fib_index))
494  {
495  rpath->frp_proto = DPO_PROTO_MPLS;
496  *payload_proto = DPO_PROTO_MPLS;
497  }
498  else if (unformat (input,
499  "l2-input-on %U",
501  &rpath->frp_sw_if_index))
502  {
503  rpath->frp_proto = DPO_PROTO_ETHERNET;
504  *payload_proto = DPO_PROTO_ETHERNET;
505  }
506  else if (unformat (input, "via-label %U",
508  &rpath->frp_local_label))
509  {
510  rpath->frp_eos = MPLS_NON_EOS;
511  rpath->frp_proto = DPO_PROTO_MPLS;
512  rpath->frp_sw_if_index = ~0;
513  }
514  else if (unformat (input, "rx-ip4 %U",
516  &rpath->frp_sw_if_index))
517  {
518  rpath->frp_proto = DPO_PROTO_IP4;
520  }
521  else if (unformat (input, "out-labels"))
522  {
523  while (unformat (input, "%U",
524  unformat_mpls_unicast_label, &out_label))
525  {
526  fib_mpls_label_t fml = {
527  .fml_value = out_label,
528  };
529  vec_add1(rpath->frp_label_stack, fml);
530  }
531  }
532  else if (unformat (input, "%U",
534  &rpath->frp_sw_if_index))
535  {
536  rpath->frp_proto = *payload_proto;
537  }
538  else
539  {
540  return (0);
541  }
542  }
543 
544  return (1);
545 }
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:236
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:197
Contribute an object that is to be used to forward BIER packets.
Definition: fib_types.h:112
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:127
ip46_address_t frp_addr
The next-hop address.
Definition: fib_types.h:471
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:103
fib_forward_chain_type_t fib_forw_chain_type_from_link_type(vnet_link_t link_type)
Convert from a adjacency&#39;s link type to chain type.
Definition: fib_types.c:336
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:482
int fib_route_path_cmp(const fib_route_path_t *rpath1, const fib_route_path_t *rpath2)
Definition: fib_types.c:213
A representation of a path as described by a route producer.
Definition: fib_types.h:455
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:383
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:359
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
#define VNET_LINKS
Definition: interface.h:257
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:99
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:460
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:334
#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:161
u8 * format_fib_protocol(u8 *s, va_list *ap)
Definition: fib_types.c:31
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:490
static const char * fib_mpls_lsp_mode_names[]
Definition: fib_types.c:28
u8 * format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
Format an LSP mode type.
Definition: fib_types.c:55
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:242
Recursion constraint of via a host prefix.
Definition: fib_types.h:309
u8 * format_fib_prefix(u8 *s, va_list *args)
Definition: fib_types.c:176
Aggregrate type for a prefix.
Definition: fib_types.h:188
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:139
A path via a UDP encap object.
Definition: fib_types.h:346
Contribute an object that is to be used to forward Ethernet packets.
Definition: fib_types.h:131
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:252
u16 fp_len
The mask length.
Definition: fib_types.h:192
Contribute an object that is to be used to forward end-of-stack MPLS packets.
Definition: fib_types.h:119
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:211
struct _unformat_input_t unformat_input_t
enum fib_mpls_lsp_mode_t_ fib_mpls_lsp_mode_t
MPLS LSP mode - only valid at the head and tail.
static const char * fib_forw_chain_names[]
Definition: fib_types.c:27
Configuration for each label value in the output-stack.
Definition: fib_types.h:410
fib_mpls_label_t * frp_label_stack
The outgoing MPLS label Stack.
Definition: fib_types.h:510
Recursion constraint of via an attahced prefix.
Definition: fib_types.h:313
fib_mpls_lsp_mode_t fml_mode
The LSP mode.
Definition: fib_types.h:420
#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:173
vnet_link_t fib_forw_chain_type_to_link_type(fib_forward_chain_type_t fct)
Convert from a chain type to the adjacency&#39;s link type.
Definition: fib_types.c:308
#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:137
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:93
mpls_label_t fml_value
The label value.
Definition: fib_types.h:415
void fib_prefix_from_ip46_addr(const ip46_address_t *addr, fib_prefix_t *pfx)
Host prefix from ip.
Definition: fib_types.c:79
mpls_label_t fp_label
Definition: fib_types.h:214
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:2750
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:543
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:547
u8 * format_fib_forw_chain_type(u8 *s, va_list *args)
Definition: fib_types.c:47
#define FIB_FORW_CHAINS
Definition: fib_types.h:140
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:236
A path that resolves via another table.
Definition: fib_types.h:362
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:478
#define FIB_MPLS_LSP_MODES
Definition: fib_types.h:397
int fib_prefix_cmp(const fib_prefix_t *p1, const fib_prefix_t *p2)
Compare two prefixes for equality.
Definition: fib_types.c:104
unsigned char u8
Definition: types.h:56
u8 * format_fib_mpls_label(u8 *s, va_list *ap)
Format an MPLS label.
Definition: fib_types.c:63
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:818
vnet_link_t fib_proto_to_link(fib_protocol_t proto)
Convert from a protocol to a link type.
Definition: fib_types.c:270
u8 fml_exp
EXP bits; valid only at imposition.
Definition: fib_types.h:430
u8 * format_vnet_link(u8 *s, va_list *ap)
Definition: fib_types.c:39
Contribute an object that is to be used to forward non-end-of-stack MPLS packets. ...
Definition: fib_types.h:108
vhost_vring_addr_t addr
Definition: vhost-user.h:83
u8 frp_weight
[un]equal cost path weight
Definition: fib_types.h:537
u32 frp_udp_encap_id
UDP encap ID.
Definition: fib_types.h:527
#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:123
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:501
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:286
#define FIB_PROTOCOLS
Definition: fib_types.h:42
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:215
u32 fib_masks[33]
Definition: ip4.h:108
enum mpls_eos_bit_t_ mpls_eos_bit_t