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