FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
mpls.c
Go to the documentation of this file.
1 /*
2  * mpls.c: mpls
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/mpls/mpls.h>
20 #include <vnet/fib/ip4_fib.h>
21 #include <vnet/fib/mpls_fib.h>
22 
23 const static char* mpls_eos_bit_names[] = MPLS_EOS_BITS;
24 
26 
27 u8 * format_mpls_unicast_label (u8 * s, va_list * args)
28 {
29  mpls_label_t label = va_arg (*args, mpls_label_t);
30 
31  switch (label) {
34  break;
37  break;
40  break;
43  break;
45  s = format (s, "%s", MPLS_IETF_ELI_STRING);
46  break;
48  s = format (s, "%s", MPLS_IETF_GAL_STRING);
49  break;
50  default:
51  s = format (s, "%d", label);
52  break;
53  }
54  return s;
55 }
56 
58 {
59  mpls_label_t *label = va_arg (*args, mpls_label_t*);
60 
65  else if (unformat (input, MPLS_IETF_ROUTER_ALERT_STRING))
67  else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_STRING))
77  else if (unformat (input, "%d", label))
78  ;
79  else
80  return (0);
81 
82  return (1);
83 }
84 
85 u8 * format_mpls_eos_bit (u8 * s, va_list * args)
86 {
87  mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t);
88 
89  ASSERT(eb <= MPLS_EOS);
90 
91  s = format(s, "%s", mpls_eos_bit_names[eb]);
92 
93  return (s);
94 }
95 
96 u8 * format_mpls_header (u8 * s, va_list * args)
97 {
98  mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t);
99 
100  return (format(s, "[%U:%d:%d:%U]",
107 }
108 
109 uword
110 unformat_mpls_header (unformat_input_t * input, va_list * args)
111 {
112  u8 ** result = va_arg (*args, u8 **);
113  mpls_unicast_header_t _h, * h = &_h;
114  u32 label, label_exp_s_ttl;
115 
116  if (! unformat (input, "MPLS %d", &label))
117  return 0;
118 
119  label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF;
120  h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl);
121 
122  /* Add gre, mpls headers to result. */
123  {
124  void * p;
125  u32 h_n_bytes = sizeof (h[0]);
126 
127  vec_add2 (*result, p, h_n_bytes);
128  clib_memcpy (p, h, h_n_bytes);
129  }
130 
131  return 1;
132 }
133 
134 uword
136  va_list * args)
137 {
138  u32 * result = va_arg (*args, u32 *);
139  u32 label;
140 
141  if (!unformat (input, "MPLS: label %d", &label))
142  return 0;
143 
144  label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;
145 
146  *result = clib_host_to_net_u32 (label);
147  return 1;
148 }
149 
151 {
152  mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
153  u32 label = h->label_exp_s_ttl;
154 
155  s = format (s, "label %d exp %d, s %d, ttl %d",
156  vnet_mpls_uc_get_label (label),
157  vnet_mpls_uc_get_exp (label),
158  vnet_mpls_uc_get_s (label),
159  vnet_mpls_uc_get_ttl (label));
160  return s;
161 }
162 
164 {
165  mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
166  mpls_unicast_header_t h_host;
167 
168  h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);
169 
171  &h_host);
172 }
173 
174 typedef struct {
181 
182 int
183 mpls_dest_cmp(void * a1, void * a2)
184 {
185  show_mpls_fib_t * r1 = a1;
186  show_mpls_fib_t * r2 = a2;
187 
188  return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
189 }
190 
191 int
192 mpls_fib_index_cmp(void * a1, void * a2)
193 {
194  show_mpls_fib_t * r1 = a1;
195  show_mpls_fib_t * r2 = a2;
196 
197  return r1->fib_index - r2->fib_index;
198 }
199 
200 int
201 mpls_label_cmp(void * a1, void * a2)
202 {
203  show_mpls_fib_t * r1 = a1;
204  show_mpls_fib_t * r2 = a2;
205 
206  return r1->label - r2->label;
207 }
208 
209 static clib_error_t *
211  unformat_input_t * input,
212  vlib_cli_command_t * cmd)
213 {
214  unformat_input_t _line_input, * line_input = &_line_input;
215  u32 table_id, is_del, is_ip, payload_proto;
216  fib_route_path_t *rpaths = NULL, rpath;
217  mpls_label_t local_label;
218  clib_error_t * error;
219  mpls_eos_bit_t eos;
220  fib_prefix_t pfx;
221 
222  error = NULL;
223  is_ip = 0;
224  table_id = 0;
225  eos = MPLS_EOS;
226  is_del = 0;
227  local_label = MPLS_LABEL_INVALID;
228  memset(&pfx, 0, sizeof(pfx));
229  payload_proto = DPO_PROTO_MPLS;
230 
231  /* Get a line of input. */
232  if (! unformat_user (input, unformat_line_input, line_input))
233  return 0;
234 
235  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
236  {
237  if (unformat (line_input, "table %d", &table_id))
238  ;
239  else if (unformat (line_input, "del"))
240  is_del = 1;
241  else if (unformat (line_input, "add"))
242  is_del = 0;
243  else if (unformat (line_input, "eos"))
244  pfx.fp_eos = MPLS_EOS;
245  else if (unformat (line_input, "non-eos"))
246  pfx.fp_eos = MPLS_NON_EOS;
247  else if (unformat (line_input, "%U/%d",
249  &pfx.fp_addr.ip4,
250  &pfx.fp_len))
251  {
253  is_ip = 1;
254  }
255  else if (unformat (line_input, "%U/%d",
257  &pfx.fp_addr.ip6,
258  &pfx.fp_len))
259  {
261  is_ip = 1;
262  }
263  else if (unformat (line_input, "via %U",
265  &rpath, &payload_proto))
266  {
267  pfx.fp_payload_proto = payload_proto;
268  vec_add1(rpaths, rpath);
269  }
270  else if (unformat (line_input, "%d", &local_label))
271  ;
272  else
273  {
274  error = clib_error_return (0, "unkown input: %U",
275  format_unformat_error, line_input);
276  goto done;
277  }
278 
279  }
280 
281  if (MPLS_LABEL_INVALID == local_label)
282  {
283  error = clib_error_return (0, "local-label required: %U",
284  format_unformat_error, input);
285  goto done;
286  }
287 
288 
289  if (is_ip)
290  {
291  u32 fib_index = fib_table_find(pfx.fp_proto, table_id);
292 
293  if (FIB_NODE_INDEX_INVALID == fib_index)
294  {
295  error = clib_error_return (0, "%U table-id %d does not exist",
296  format_fib_protocol, pfx.fp_proto, table_id);
297  goto done;
298  }
299 
300  if (is_del)
301  {
302  fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
303  }
304  else
305  {
306  fib_table_entry_local_label_add(fib_index, &pfx, local_label);
307  }
308  }
309  else
310  {
311  fib_node_index_t fib_index;
312  u32 fi;
313 
314  if (NULL == rpaths)
315  {
316  error = clib_error_return(0 , "no paths");
317  goto done;
318  }
319 
321  pfx.fp_len = 21;
322  pfx.fp_label = local_label;
323  pfx.fp_payload_proto = rpaths[0].frp_proto;
324 
325  /*
326  * the CLI parsing stored table Ids, swap to FIB indicies
327  */
328  if (FIB_NODE_INDEX_INVALID == rpath.frp_sw_if_index)
329  {
331  rpaths[0].frp_fib_index);
332 
333  if (~0 == fi)
334  {
335  error = clib_error_return(0 , "%U Via table %d does not exist",
337  rpaths[0].frp_fib_index);
338  goto done;
339  }
340  rpaths[0].frp_fib_index = fi;
341  }
342 
343  fib_index = mpls_fib_index_from_table_id(table_id);
344 
345  if (FIB_NODE_INDEX_INVALID == fib_index)
346  {
347  error = clib_error_return (0, "MPLS table-id %d does not exist",
348  table_id);
349  goto done;
350  }
351 
352  if (is_del)
353  {
355  &pfx,
357  rpaths);
358  }
359  else
360  {
361  fib_node_index_t lfe;
362 
363  lfe = fib_table_entry_path_add2(fib_index,
364  &pfx,
367  rpaths);
368 
369  if (FIB_NODE_INDEX_INVALID == lfe)
370  {
371  error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d",
372  format_mpls_unicast_label, local_label,
373  format_mpls_eos_bit, eos,
374  table_id);
375  goto done;
376  }
377  }
378  }
379 
380 done:
381  unformat_free (line_input);
382 
383  return error;
384 }
385 
386 VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
387  .path = "mpls local-label",
388  .function = vnet_mpls_local_label,
389  .short_help = "mpls local-label [add|del] <label-value> [eos|non-eos] via [next-hop-address] [next-hop-interface] [next-hop-table <value>] [weight <value>] [preference <value>] [udp-encap-id <value>] [ip4-lookup-in-table <value>] [ip6-lookup-in-table <value>] [mpls-lookup-in-table <value>] [resolve-via-host] [resolve-via-attached] [rx-ip4 <interface>] [out-labels <value value value>]",
390 };
391 
392 clib_error_t *
394  unformat_input_t * main_input,
395  vlib_cli_command_t * cmdo)
396 {
397  unformat_input_t _line_input, *line_input = &_line_input;
398  clib_error_t *error = NULL;
399  u32 table_id, is_add;
400  u8 *name = NULL;
401 
402  is_add = 1;
403  table_id = ~0;
404 
405  /* Get a line of input. */
406  if (!unformat_user (main_input, unformat_line_input, line_input))
407  return 0;
408 
409  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
410  {
411  if (unformat (line_input, "%d", &table_id))
412  ;
413  else if (unformat (line_input, "del"))
414  is_add = 0;
415  else if (unformat (line_input, "add"))
416  is_add = 1;
417  else if (unformat (line_input, "name %s", &name))
418  ;
419  else
420  {
421  error = unformat_parse_error (line_input);
422  goto done;
423  }
424  }
425 
426  if (~0 == table_id)
427  {
428  error = clib_error_return (0, "No table id");
429  goto done;
430  }
431  else
432  {
433  if (is_add)
434  {
435  mpls_table_create (table_id, 0, name);
436  }
437  else
438  {
439  mpls_table_delete (table_id, 0);
440  }
441  }
442 
443  done:
444  unformat_free (line_input);
445  return error;
446 }
447 
448 /* *INDENT-ON* */
449 /*?
450  * This command is used to add or delete MPLS Tables. All
451  * Tables must be explicitly added before that can be used,
452  * Including the default table.
453  ?*/
454 /* *INDENT-OFF* */
455 VLIB_CLI_COMMAND (mpls_table_command, static) = {
456  .path = "mpls table",
457  .short_help = "mpls table [add|del] <table-id>",
458  .function = vnet_mpls_table_cmd,
459  .is_mp_safe = 1,
460 };
461 
462 int
464 {
465  // FIXME
466  return 0;
467 }
468 
469 static clib_error_t *
471 {
472  clib_error_t * error;
473 
474  if ((error = vlib_call_init_function (vm, ip_main_init)))
475  return error;
476 
478 }
479 
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:181
uword unformat_mpls_unicast_label(unformat_input_t *input, va_list *args)
Definition: mpls.c:57
A representation of a path as described by a route producer.
Definition: fib_types.h:377
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
#define NULL
Definition: clib.h:55
fib_node_index_t fib_table_entry_path_add2(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, fib_route_path_t *rpath)
Add n paths to an entry (aka route) in the FIB.
Definition: fib_table.c:539
u32 mpls_label_t
A label value only, i.e.
Definition: packet.h:24
#define MPLS_IETF_IMPLICIT_NULL_LABEL
Definition: mpls_types.h:30
From the CLI.
Definition: fib_entry.h:74
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
#define MPLS_IETF_ROUTER_ALERT_LABEL
Definition: mpls_types.h:28
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:557
u8 * format_mpls_unicast_label(u8 *s, va_list *args)
Definition: mpls.c:27
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
u8 * format_mpls_unicast_header_host_byte_order(u8 *s, va_list *args)
Definition: mpls.c:150
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
int mpls_dest_cmp(void *a1, void *a2)
Definition: mpls.c:183
void fib_table_entry_local_label_remove(u32 fib_index, const fib_prefix_t *prefix, mpls_label_t label)
remove a MPLS local label for the prefix/route.
Definition: fib_table.c:899
#define MPLS_IETF_IPV4_EXPLICIT_NULL_BRIEF_STRING
Definition: mpls_types.h:36
u8 * format_fib_protocol(u8 *s, va_list *ap)
Definition: fib_types.c:30
dpo_proto_t fp_payload_proto
This protocol determines the payload protocol of packets that will be forwarded by this entry once th...
Definition: fib_types.h:205
unformat_function_t unformat_ip4_address
Definition: format.h:76
#define MPLS_IETF_ROUTER_ALERT_STRING
Definition: mpls_types.h:39
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
int mpls_fib_reset_labels(u32 fib_id)
Definition: mpls.c:463
Aggregrate type for a prefix.
Definition: fib_types.h:172
#define clib_error_return(e, args...)
Definition: error.h:99
static const char * mpls_eos_bit_names[]
Definition: mpls.c:23
#define MPLS_IETF_ROUTER_ALERT_BRIEF_STRING
Definition: mpls_types.h:40
static u32 vnet_mpls_uc_get_ttl(mpls_label_t label_exp_s_ttl)
Definition: packet.h:92
#define MPLS_EOS_BITS
Definition: packet.h:40
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1028
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
#define vlib_call_init_function(vm, x)
Definition: init.h:162
#define MPLS_IETF_IPV6_EXPLICIT_NULL_BRIEF_STRING
Definition: mpls_types.h:42
Definition: fib_entry.h:238
unformat_function_t unformat_line_input
Definition: format.h:281
#define MPLS_IETF_GAL_STRING
Definition: mpls_types.h:45
void mpls_table_delete(u32 table_id, u8 is_api)
Definition: mpls_api.c:63
#define MPLS_IETF_ELI_LABEL
Definition: mpls_types.h:31
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:195
u32 entry_index
Definition: mpls.c:176
struct _unformat_input_t unformat_input_t
clib_error_t * vnet_mpls_table_cmd(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmdo)
Definition: mpls.c:393
void fib_table_entry_path_remove2(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_route_path_t *rpath)
Remove n paths to an entry (aka route) in the FIB.
Definition: fib_table.c:583
static u32 vnet_mpls_uc_get_label(mpls_label_t label_exp_s_ttl)
Definition: packet.h:77
uword unformat_mpls_header(unformat_input_t *input, va_list *args)
Definition: mpls.c:110
#define MPLS_IETF_IPV4_EXPLICIT_NULL_STRING
Definition: mpls_types.h:35
unformat_function_t unformat_ip6_address
Definition: format.h:94
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
fib_node_index_t fib_table_entry_local_label_add(u32 fib_index, const fib_prefix_t *prefix, mpls_label_t label)
Add a MPLS local label for the prefix/route.
Definition: fib_table.c:872
mpls_main_t mpls_main
Definition: mpls.c:25
vlib_main_t * vm
Definition: buffer.c:283
vec_header_t h
Definition: buffer.c:282
#define MPLS_LABEL_INVALID
Definition: mpls_types.h:48
#define MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL
Definition: mpls_types.h:27
#define clib_memcpy(a, b, c)
Definition: string.h:75
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
#define MPLS_IETF_IMPLICIT_NULL_BRIEF_STRING
Definition: mpls_types.h:38
u32 mpls_fib_index_from_table_id(u32 table_id)
Definition: mpls_fib.c:73
mpls_label_t fp_label
Definition: fib_types.h:198
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
static clib_error_t * mpls_input_init(vlib_main_t *vm)
Definition: mpls_input.c:298
u32 fib_index
Definition: mpls.c:175
#define MPLS_IETF_IPV6_EXPLICIT_NULL_STRING
Definition: mpls_types.h:41
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static clib_error_t * vnet_mpls_local_label(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: mpls.c:210
#define MPLS_IETF_GAL_LABEL
Definition: mpls_types.h:32
void mpls_table_create(u32 table_id, u8 is_api, const u8 *name)
Definition: mpls_api.c:269
uword unformat_mpls_label_net_byte_order(unformat_input_t *input, va_list *args)
Definition: mpls.c:135
#define MPLS_IETF_ELI_STRING
Definition: mpls_types.h:43
mpls_label_t label_exp_s_ttl
Definition: packet.h:31
#define MPLS_IETF_IMPLICIT_NULL_STRING
Definition: mpls_types.h:37
u64 uword
Definition: types.h:112
static clib_error_t * mpls_init(vlib_main_t *vm)
Definition: mpls.c:470
#define unformat_parse_error(input)
Definition: format.h:267
u8 * format_mpls_unicast_header_net_byte_order(u8 *s, va_list *args)
Definition: mpls.c:163
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
static u32 vnet_mpls_uc_get_s(mpls_label_t label_exp_s_ttl)
Definition: packet.h:87
unsigned char u8
Definition: types.h:56
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
#define MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL
Definition: mpls_types.h:29
u8 * format_dpo_proto(u8 *s, va_list *args)
format a DPO protocol
Definition: dpo.c:174
u8 * format_mpls_eos_bit(u8 *s, va_list *args)
Definition: mpls.c:85
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
int mpls_label_cmp(void *a1, void *a2)
Definition: mpls.c:201
static u32 vnet_mpls_uc_get_exp(mpls_label_t label_exp_s_ttl)
Definition: packet.h:82
u32 frp_fib_index
The FIB index to lookup the nexthop Only valid for recursive paths.
Definition: fib_types.h:423
int mpls_fib_index_cmp(void *a1, void *a2)
Definition: mpls.c:192
u8 * format_mpls_header(u8 *s, va_list *args)
Definition: mpls.c:96
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
enum mpls_eos_bit_t_ mpls_eos_bit_t