FD.io VPP  v17.04.2-2-ga8f93f8
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  fib_prefix_t *pfx)
70 {
72  pfx->fp_len = 21;
73  pfx->fp_label = label;
74  pfx->fp_eos = MPLS_NON_EOS;
75 }
76 
77 int
79  const fib_prefix_t *p2)
80 {
81  int res;
82 
83  res = (p1->fp_proto - p2->fp_proto);
84 
85  if (0 == res)
86  {
87  switch (p1->fp_proto)
88  {
89  case FIB_PROTOCOL_IP4:
90  case FIB_PROTOCOL_IP6:
91  res = (p1->fp_len - p2->fp_len);
92 
93  if (0 == res)
94  {
95  res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
96  }
97  break;
98  case FIB_PROTOCOL_MPLS:
99  res = (p1->fp_label - p2->fp_label);
100 
101  if (0 == res)
102  {
103  res = (p1->fp_eos - p2->fp_eos);
104  }
105  break;
106  }
107  }
108 
109  return (res);
110 }
111 
112 int
114  const fib_prefix_t *p2)
115 {
116  switch (p1->fp_proto)
117  {
118  case FIB_PROTOCOL_IP4:
120  &p1->fp_addr.ip4,
121  &p2->fp_addr.ip4,
122  p1->fp_len));
123  case FIB_PROTOCOL_IP6:
125  &p1->fp_addr.ip6,
126  &p2->fp_addr.ip6,
127  p1->fp_len));
128  case FIB_PROTOCOL_MPLS:
129  break;
130  }
131  return (0);
132 }
133 
134 int
136 {
137  switch (prefix->fp_proto)
138  {
139  case FIB_PROTOCOL_IP4:
140  return (prefix->fp_len == 32);
141  case FIB_PROTOCOL_IP6:
142  return (prefix->fp_len == 128);
143  case FIB_PROTOCOL_MPLS:
144  return (!0);
145  }
146  return (0);
147 }
148 
149 u8 *
150 format_fib_prefix (u8 * s, va_list * args)
151 {
152  fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
153 
154  /*
155  * protocol specific so it prints ::/0 correctly.
156  */
157  switch (fp->fp_proto)
158  {
159  case FIB_PROTOCOL_IP6:
160  {
161  ip6_address_t p6 = fp->fp_addr.ip6;
162 
164  s = format (s, "%U", format_ip6_address, &p6);
165  break;
166  }
167  case FIB_PROTOCOL_IP4:
168  {
169  ip4_address_t p4 = fp->fp_addr.ip4;
170  p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
171 
172  s = format (s, "%U", format_ip4_address, &p4);
173  break;
174  }
175  case FIB_PROTOCOL_MPLS:
176  s = format (s, "%U:%U",
179  break;
180  }
181  s = format (s, "/%d", fp->fp_len);
182 
183  return (s);
184 }
185 
186 int
188  const fib_route_path_t *rpath2)
189 {
190  int res;
191 
192  res = ip46_address_cmp(&rpath1->frp_addr,
193  &rpath2->frp_addr);
194 
195  if (0 != res) return (res);
196 
197  if (~0 != rpath1->frp_sw_if_index &&
198  ~0 != rpath2->frp_sw_if_index)
199  {
201  rpath1->frp_sw_if_index,
202  rpath2->frp_sw_if_index);
203  }
204  else
205  {
206  res = rpath1->frp_sw_if_index - rpath2->frp_sw_if_index;
207  }
208 
209  if (0 != res) return (res);
210 
211  if (ip46_address_is_zero(&rpath1->frp_addr))
212  {
213  res = rpath1->frp_fib_index - rpath2->frp_fib_index;
214  }
215 
216  return (res);
217 }
218 
221 {
222  switch (fib_proto)
223  {
224  case FIB_PROTOCOL_IP6:
225  return (DPO_PROTO_IP6);
226  case FIB_PROTOCOL_IP4:
227  return (DPO_PROTO_IP4);
228  case FIB_PROTOCOL_MPLS:
229  return (DPO_PROTO_MPLS);
230  }
231  ASSERT(0);
232  return (0);
233 }
234 
237 {
238  switch (dpo_proto)
239  {
240  case DPO_PROTO_IP6:
241  return (FIB_PROTOCOL_IP6);
242  case DPO_PROTO_IP4:
243  return (FIB_PROTOCOL_IP4);
244  case DPO_PROTO_MPLS:
245  return (FIB_PROTOCOL_MPLS);
246  default:
247  break;
248  }
249  ASSERT(0);
250  return (0);
251 }
252 
255 {
256  switch (proto)
257  {
258  case FIB_PROTOCOL_IP4:
259  return (VNET_LINK_IP4);
260  case FIB_PROTOCOL_IP6:
261  return (VNET_LINK_IP6);
262  case FIB_PROTOCOL_MPLS:
263  return (VNET_LINK_MPLS);
264  }
265  ASSERT(0);
266  return (0);
267 }
268 
271 {
272  switch (proto)
273  {
274  case DPO_PROTO_IP4:
276  case DPO_PROTO_IP6:
278  case DPO_PROTO_MPLS:
280  case DPO_PROTO_ETHERNET:
282  case DPO_PROTO_NSH:
283  return (FIB_FORW_CHAIN_TYPE_NSH);
284  }
285  ASSERT(0);
287 }
288 
291 {
292  switch (fct)
293  {
296  return (VNET_LINK_IP4);
299  return (VNET_LINK_IP6);
301  return (VNET_LINK_ETHERNET);
303  return (VNET_LINK_NSH);
305  /*
306  * insufficient information to to convert
307  */
308  ASSERT(0);
309  break;
311  return (VNET_LINK_MPLS);
312  }
313  return (VNET_LINK_IP4);
314 }
315 
318 {
319  switch (fct)
320  {
323  return (DPO_PROTO_IP4);
326  return (DPO_PROTO_IP6);
328  return (DPO_PROTO_ETHERNET);
330  return (DPO_PROTO_NSH);
333  return (DPO_PROTO_MPLS);
334  }
335  return (DPO_PROTO_IP4);
336 }
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:169
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:105
ip46_address_t frp_addr
The next-hop address.
Definition: fib_types.h:322
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:85
static const char * fib_protocol_names[]
Definition: fib_types.c:25
int fib_route_path_cmp(const fib_route_path_t *rpath1, const fib_route_path_t *rpath2)
Definition: fib_types.c:187
A representation of a path as described by a route producer.
Definition: fib_types.h:308
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
u8 * format_vnet_link(u8 *s, va_list ap)
Definition: fib_types.c:38
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:317
word vnet_sw_interface_compare(vnet_main_t *vnm, uword sw_if_index0, uword sw_if_index1)
Definition: interface.c:1112
#define VNET_LINKS
Definition: interface.h:246
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:81
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:418
#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:135
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
void fib_prefix_from_mpls_label(mpls_label_t label, fib_prefix_t *pfx)
Big train switch; FIB debugs on or off.
Definition: fib_types.c:68
format_function_t format_ip4_address
Definition: format.h:79
u32 frp_sw_if_index
The interface.
Definition: fib_types.h:334
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:221
u8 * format_fib_prefix(u8 *s, va_list *args)
Definition: fib_types.c:150
Aggregrate type for a prefix.
Definition: fib_types.h:160
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:113
Contribute an object that is to be used to forward Ethernet packets.
Definition: fib_types.h:109
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:236
u16 fp_len
The mask length.
Definition: fib_types.h:164
Contribute an object that is to be used to forward end-of-stack MPLS packets.
Definition: fib_types.h:97
format_function_t format_mpls_eos_bit
Definition: mpls.h:89
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:183
static const char * fib_forw_chain_names[]
Definition: fib_types.c:27
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
ip6_address_t fib_masks[129]
Definition: ip6.h:160
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:290
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:115
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:186
#define ASSERT(truth)
ip6_main_t ip6_main
Definition: ip6_forward.c:2846
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
enum fib_forward_chain_type_t_ fib_forward_chain_type_t
FIB output chain type.
u8 * format_fib_forw_chain_type(u8 *s, va_list *args)
Definition: fib_types.c:46
#define FIB_FORW_CHAINS
Definition: fib_types.h:118
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:220
u8 * format_fib_protocol(u8 *s, va_list ap)
Definition: fib_types.c:30
format_function_t format_mpls_unicast_label
Definition: mpls.h:91
int fib_prefix_cmp(const fib_prefix_t *p1, const fib_prefix_t *p2)
Compare two prefixes for equality.
Definition: fib_types.c:78
unsigned char u8
Definition: types.h:56
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1117
vnet_link_t fib_proto_to_link(fib_protocol_t proto)
Convert from a protocol to a link type.
Definition: fib_types.c:254
Contribute an object that is to be used to forward non-end-of-stack MPLS packets. ...
Definition: fib_types.h:90
vhost_vring_addr_t addr
Definition: vhost-user.h:84
#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:101
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:339
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:270
#define FIB_PROTOCOLS
Definition: fib_types.h:40
mpls_eos_bit_t fp_eos
Definition: fib_types.h:187
u32 fib_masks[33]
Definition: ip4.h:117