aboutsummaryrefslogtreecommitdiffstats
path: root/vnet/vnet/mpls/mpls.c
blob: ac35e5d4144de1e73d083aa43c1103092a38eb14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * mpls.c: mpls
 *
 * Copyright (c) 2012 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/vnet.h>
#include <vnet/mpls/mpls.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/fib/mpls_fib.h>

const static char* mpls_eos_bit_names[] = MPLS_EOS_BITS;

mpls_main_t mpls_main;

u8 * format_mpls_unicast_label (u8 * s, va_list * args)
{
  mpls_label_t label = va_arg (*args, mpls_label_t);

  switch (label) {
  case MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL:
      s = format (s, "%s", MPLS_IETF_IPV4_EXPLICIT_NULL_STRING);
      break;
  case MPLS_IETF_ROUTER_ALERT_LABEL:
      s = format (s, "%s", MPLS_IETF_ROUTER_ALERT_STRING);
      break;
  case MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL:
      s = format (s, "%s", MPLS_IETF_IPV6_EXPLICIT_NULL_STRING);
      break;
  case MPLS_IETF_IMPLICIT_NULL_LABEL:
      s = format (s, "%s", MPLS_IETF_IMPLICIT_NULL_STRING);
      break;
  case MPLS_IETF_ELI_LABEL:
      s = format (s, "%s", MPLS_IETF_ELI_STRING);
      break;
  case MPLS_IETF_GAL_LABEL:
      s = format (s, "%s", MPLS_IETF_GAL_STRING);
      break;
  default:
      s = format (s, "%d", label);
      break;
  }
  return s;
}

uword unformat_mpls_unicast_label (unformat_input_t * input, va_list * args)
{
  mpls_label_t *label = va_arg (*args, mpls_label_t*);
  
  if (unformat (input, MPLS_IETF_IPV4_EXPLICIT_NULL_STRING))
      *label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
  else if (unformat (input, MPLS_IETF_IPV6_EXPLICIT_NULL_STRING))
      *label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
  else if (unformat (input, MPLS_IETF_ROUTER_ALERT_STRING))
      *label = MPLS_IETF_ROUTER_ALERT_LABEL;
  else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_STRING))
      *label = MPLS_IETF_IMPLICIT_NULL_LABEL;
  else if (unformat (input, "%d", label))
      ;

  return (1);
}

u8 * format_mpls_eos_bit (u8 * s, va_list * args)
{
  mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t);

  ASSERT(eb <= MPLS_EOS);

  s = format(s, "%s", mpls_eos_bit_names[eb]);

  return (s);
}

u8 * format_mpls_header (u8 * s, va_list * args)
{
  mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t);

  return (format(s, "[%U:%d:%d:%U]",
		 format_mpls_unicast_label, 
		 vnet_mpls_uc_get_label(hdr.label_exp_s_ttl),
		 vnet_mpls_uc_get_ttl(hdr.label_exp_s_ttl),
		 vnet_mpls_uc_get_exp(hdr.label_exp_s_ttl),
		 format_mpls_eos_bit,
		 vnet_mpls_uc_get_s(hdr.label_exp_s_ttl)));
}

uword
unformat_mpls_header (unformat_input_t * input, va_list * args)
{
  u8 ** result = va_arg (*args, u8 **);
  mpls_unicast_header_t _h, * h = &_h;
  u32 label, label_exp_s_ttl;

  if (! unformat (input, "MPLS %d", &label))
    return 0;

  label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF;
  h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl);

  /* Add gre, mpls headers to result. */
  {
    void * p;
    u32 h_n_bytes = sizeof (h[0]);

    vec_add2 (*result, p, h_n_bytes);
    clib_memcpy (p, h, h_n_bytes);
  }

  return 1;
}

u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  mpls_eth_tx_trace_t * t = va_arg (*args, mpls_eth_tx_trace_t *);
  mpls_main_t * mm = &mpls_main;
    
  if (t->lookup_miss)
    s = format (s, "MPLS: lookup miss");
  else
    {
      s = format (s, "MPLS: tunnel %d labels %U len %d tx_sw_index %d dst %U",
                  t->tunnel_id,
                  format_mpls_encap_index, mm, t->mpls_encap_index, 
                  clib_net_to_host_u16 (t->length),
                  t->tx_sw_if_index, 
                  format_ethernet_address, t->dst);
    }
  return s;
}

u8 * format_mpls_eth_header_with_length (u8 * s, va_list * args)
{
  ethernet_header_t * h = va_arg (*args, ethernet_header_t *);
  mpls_unicast_header_t * m = (mpls_unicast_header_t *)(h+1);
  u32 max_header_bytes = va_arg (*args, u32);
  uword header_bytes;

  header_bytes = sizeof (h[0]);
  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
    return format (s, "ethernet header truncated");

  s = format 
    (s, "ETHERNET-MPLS label %d", 
     vnet_mpls_uc_get_label (clib_net_to_host_u32 (m->label_exp_s_ttl)));

  return s;
}

uword
unformat_mpls_label_net_byte_order (unformat_input_t * input,
                                        va_list * args)
{
  u32 * result = va_arg (*args, u32 *);
  u32 label;

  if (!unformat (input, "MPLS: label %d", &label))
    return 0;

  label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;

  *result = clib_host_to_net_u32 (label);
  return 1;
}

mpls_encap_t * 
mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address)
{
  uword * p;
  mpls_encap_t * e;
  u64 key;

  key = ((u64)rx_fib<<32) | ((u64) dst_address);
  p = hash_get (mm->mpls_encap_by_fib_and_dest, key);

  if (!p)
    return 0;

  e = pool_elt_at_index (mm->encaps, p[0]);
  return e;
}

int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id, 
                             u32 *labels_host_byte_order,
                             u32 policy_tunnel_index,
                             int no_dst_hash, u32 * indexp, int is_add)
{
  mpls_main_t * mm = &mpls_main;
  ip4_main_t * im = &ip4_main;
  mpls_encap_t * e;
  u32 label_net_byte_order, label_host_byte_order;
  u32 fib_index;
  u64 key;
  uword *p;
  int i;
  
  p = hash_get (im->fib_index_by_table_id, fib_id);
  if (! p)
    return VNET_API_ERROR_NO_SUCH_FIB;
  
  fib_index = p[0];
  
  key = ((u64)fib_index<<32) | ((u64) dest->as_u32);
  
  if (is_add)
    {
      pool_get (mm->encaps, e);
      memset (e, 0, sizeof (*e));
      
      for (i = 0; i < vec_len (labels_host_byte_order); i++)
        {
          mpls_unicast_header_t h;
          label_host_byte_order = labels_host_byte_order[i];
          
          /* Reformat label into mpls_unicast_header_t */
          label_host_byte_order <<= 12;
	  // FIXME NEOS AND EOS
          //if (i == vec_len(labels_host_byte_order) - 1)
          //  label_host_byte_order |= 1<<8;            /* S=1 */
          label_host_byte_order |= 0xff;            /* TTL=FF */
          label_net_byte_order = clib_host_to_net_u32 (label_host_byte_order);
          h.label_exp_s_ttl = label_net_byte_order;
          vec_add1 (e->labels, h);
        }
      if (no_dst_hash == 0)
        hash_set (mm->mpls_encap_by_fib_and_dest, key, e - mm->encaps);
      if (indexp)
        *indexp = e - mm->encaps;
      if (policy_tunnel_index != ~0)
        return vnet_mpls_policy_tunnel_add_rewrite (mm, e, policy_tunnel_index);
    }
  else
    {
      p = hash_get (mm->mpls_encap_by_fib_and_dest, key);
      if (!p)
        return VNET_API_ERROR_NO_SUCH_LABEL;
      
      e = pool_elt_at_index (mm->encaps, p[0]);
      
      vec_free (e->labels);
      vec_free (e->rewrite);
      pool_put(mm->encaps, e);
      
      if (no_dst_hash == 0)
        hash_unset (mm->mpls_encap_by_fib_and_dest, key);    
    }
  return 0;
}

static clib_error_t *
mpls_add_encap_command_fn (vlib_main_t * vm,
                           unformat_input_t * input,
                           vlib_cli_command_t * cmd)
{
  u32 fib_id;
  u32 *labels = 0;
  u32 this_label;
  ip4_address_t dest;
  u32 policy_tunnel_index = ~0;
  int no_dst_hash = 0;
  int rv;
  int fib_set = 0;
  int dest_set = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "fib %d", &fib_id))
        fib_set = 1;
      else if (unformat (input, "dest %U", unformat_ip4_address, &dest))
        dest_set = 1;
      else if (unformat (input, "no-dst-hash"))
        no_dst_hash = 1;
      else if (unformat (input, "label %d", &this_label))
        vec_add1 (labels, this_label);
      else if (unformat (input, "policy-tunnel %d", &policy_tunnel_index))
        ;
      else
        break;
    }

  if (fib_set == 0)
    return clib_error_return (0, "fib-id missing");
  if (dest_set == 0)
    return clib_error_return (0, "destination IP address missing");
  if (vec_len (labels) == 0)
    return clib_error_return (0, "label stack missing");
  
  rv = vnet_mpls_add_del_encap (&dest, fib_id, labels, 
                                policy_tunnel_index, 
                                no_dst_hash, 0 /* indexp */,
                                1 /* is_add */);
  vec_free (labels);

  switch (rv)
    {
    case 0:
      break;

    case VNET_API_ERROR_NO_SUCH_FIB:
      return clib_error_return (0, "fib id %d unknown", fib_id);
      
    default:
      return clib_error_return (0, "vnet_mpls_add_del_encap returned %d", 
                                rv);
    }

  return 0;
}

VLIB_CLI_COMMAND (mpls_add_encap_command, static) = {
  .path = "mpls encap add",
  .short_help = 
  "mpls encap add label <label> ... fib <id> dest <ip4-address>",
  .function = mpls_add_encap_command_fn,
};

u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args)
{
  mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
  u32 label = h->label_exp_s_ttl;
  
  s = format (s, "label %d exp %d, s %d, ttl %d",
              vnet_mpls_uc_get_label (label),
              vnet_mpls_uc_get_exp (label),
              vnet_mpls_uc_get_s (label),
              vnet_mpls_uc_get_ttl (label));
  return s;
}

u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args)
{
  mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
  mpls_unicast_header_t h_host;

  h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);

  return format (s, "%U", format_mpls_unicast_header_host_byte_order,
                 &h_host);
}

static clib_error_t *
mpls_del_encap_command_fn (vlib_main_t * vm,
		 unformat_input_t * input,
		 vlib_cli_command_t * cmd)
{
  u32 fib_id;
  ip4_address_t dest;
  int rv;
  
  if (unformat (input, "fib %d dest %U", &fib_id, 
                unformat_ip4_address, &dest)) 
    {
      rv = vnet_mpls_add_del_encap (&dest, fib_id, 0 /* labels */, 
                                    ~0 /* policy_tunnel_index */,
                                    0 /* no_dst_hash */,
                                    0 /* indexp */,
                                    0 /* is_add */);
      switch (rv)
        {
        case VNET_API_ERROR_NO_SUCH_FIB:
          return clib_error_return (0, "fib id %d unknown", fib_id);
        case VNET_API_ERROR_NO_SUCH_ENTRY:
          return clib_error_return (0, "dest %U not in fib %d",
                                    format_ip4_address, &dest, fib_id);
        default:
          break;
        }
      return 0;
    }
  else
    return clib_error_return (0, "unknown input `%U'",
                              format_unformat_error, input);
}

VLIB_CLI_COMMAND (mpls_del_encap_command, static) = {
  .path = "mpls encap delete",
  .short_help = "mpls encap delete fib <id> dest <ip4-address>",
  .function = mpls_del_encap_command_fn,
};

int
mpls_dest_cmp(void * a1, void * a2)
{
  show_mpls_fib_t * r1 = a1;
  show_mpls_fib_t * r2 = a2;

  return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
}

int
mpls_fib_index_cmp(void * a1, void * a2)
{
  show_mpls_fib_t * r1 = a1;
  show_mpls_fib_t * r2 = a2;

  return r1->fib_index - r2->fib_index;
}

int
mpls_label_cmp(void * a1, void * a2)
{
  show_mpls_fib_t * r1 = a1;
  show_mpls_fib_t * r2 = a2;

  return r1->label - r2->label;
}

static clib_error_t *
show_mpls_fib_command_fn (vlib_main_t * vm,
		 unformat_input_t * input,
		 vlib_cli_command_t * cmd)
{
  u64 key; 
  u32 value;
  show_mpls_fib_t *records = 0;
  show_mpls_fib_t *s;
  mpls_main_t * mm = &mpls_main;
  ip4_fib_t * rx_fib;

  hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest, 
  ({
    vec_add2 (records, s, 1);
    s->fib_index = (u32)(key>>32);
    s->dest = (u32)(key & 0xFFFFFFFF);
    s->entry_index = (u32) value;
  }));

  if (!vec_len(records))
    {
      vlib_cli_output (vm, "MPLS encap table empty");
    }
  /* sort output by dst address within fib */
  vec_sort_with_function (records, mpls_dest_cmp);
  vec_sort_with_function (records, mpls_fib_index_cmp);
  vlib_cli_output (vm, "MPLS encap table");
  vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
  vec_foreach (s, records)
    {
      rx_fib = ip4_fib_get (s->fib_index);
      vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id, 
                       format_ip4_address, &s->dest,
                       format_mpls_encap_index, mm, s->entry_index);
    }

  vec_free(records);
  return 0;
}

VLIB_CLI_COMMAND (show_mpls_fib_command, static) = {
    .path = "show mpls encap",
    .short_help = "show mpls encap",
    .function = show_mpls_fib_command_fn,
};

static clib_error_t *
vnet_mpls_local_label (vlib_main_t * vm,
                       unformat_input_t * input,
                       vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, * line_input = &_line_input;
  fib_route_path_t *rpaths = NULL, rpath;
  clib_error_t * error = 0;
  u32 table_id, is_del, is_ip;
  fib_prefix_t pfx;
  mpls_label_t local_label;
  mpls_eos_bit_t eos;

  is_ip = 0;
  table_id = 0;
  eos = MPLS_EOS;
  is_del = 0;
  local_label = MPLS_LABEL_INVALID;
  memset(&pfx, 0, sizeof(pfx));

   /* Get a line of input. */
  if (! unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      memset(&rpath, 0, sizeof(rpath));

      if (unformat (line_input, "table %d", &table_id))
	;
      else if (unformat (line_input, "del"))
	is_del = 1;
      else if (unformat (line_input, "add"))
	is_del = 0;
      else if (unformat (line_input, "eos"))
	pfx.fp_eos = MPLS_EOS;
      else if (unformat (line_input, "non-eos"))
	pfx.fp_eos = MPLS_NON_EOS;
      else if (unformat (line_input, "%U/%d",
			 unformat_ip4_address,
			 &pfx.fp_addr.ip4,
			 &pfx.fp_len))
      {
	  pfx.fp_proto = FIB_PROTOCOL_IP4;
          is_ip = 1;
      }
      else if (unformat (line_input, "%U/%d",
			 unformat_ip6_address,
			 &pfx.fp_addr.ip6,
			 &pfx.fp_len))
      {
	  pfx.fp_proto = FIB_PROTOCOL_IP6;
          is_ip = 1;
      }
      else if (unformat (line_input, "%d", &local_label))
	;
      else if (unformat (line_input,
			 "ip4-lookup-in-table %d",
			 &rpath.frp_fib_index))
      {
	  rpath.frp_label = MPLS_LABEL_INVALID;
          rpath.frp_proto = FIB_PROTOCOL_IP4;
          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
	  pfx.fp_payload_proto = FIB_PROTOCOL_IP4;
	  vec_add1(rpaths, rpath);
      }
      else if (unformat (line_input,
			 "ip6-lookup-in-table %d",
			 &rpath.frp_fib_index))
      {
	  rpath.frp_label = MPLS_LABEL_INVALID;
          rpath.frp_proto = FIB_PROTOCOL_IP6;
          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
	  vec_add1(rpaths, rpath);
	  pfx.fp_payload_proto = FIB_PROTOCOL_IP6;
      }
      else if (unformat (line_input,
			 "mpls-lookup-in-table %d",
			 &rpath.frp_fib_index))
      {
	  rpath.frp_label = MPLS_LABEL_INVALID;
          rpath.frp_proto = FIB_PROTOCOL_MPLS;
          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
	  pfx.fp_payload_proto = FIB_PROTOCOL_MPLS;
	  vec_add1(rpaths, rpath);
      }
      else
      {
          error = clib_error_return (0, "unkown input: %U",
                                     format_unformat_error, input);
          goto done;
      }

    }

  if (MPLS_LABEL_INVALID == local_label)
  {
      error = clib_error_return (0, "local-label required: %U",
				 format_unformat_error, input);
      goto done;
  }


  if (is_ip)
  {
      u32 fib_index = fib_table_find(pfx.fp_proto, table_id);

      if (FIB_NODE_INDEX_INVALID == fib_index)
      {
          error = clib_error_return (0, "%U table-id %d does not exist",
                                     format_fib_protocol, pfx.fp_proto, table_id);
          goto done;
      }

      if (is_del)
      {
          fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
      }
      else
      {
          fib_table_entry_local_label_add(fib_index, &pfx, local_label);
      }
  }
  else
  {
      fib_node_index_t lfe, fib_index;
      u32 fi;

      pfx.fp_proto = FIB_PROTOCOL_MPLS;
      pfx.fp_len = 21;
      pfx.fp_label = local_label;

      /*
       * the CLI parsing stored table Ids, swap to FIB indicies
       */
      fi = fib_table_id_find_fib_index(pfx.fp_payload_proto,
				       rpaths[0].frp_fib_index);

      if (~0 == fi)
      {
	  error = clib_error_return(0 , "%U Via table %d does not exist",
				    format_fib_protocol, pfx.fp_payload_proto,
				    rpaths[0].frp_fib_index);
	  goto done;
      }
      rpaths[0].frp_fib_index = fi;

      fib_index = mpls_fib_index_from_table_id(table_id);

      if (FIB_NODE_INDEX_INVALID == fib_index)
      {
          error = clib_error_return (0, "MPLS table-id %d does not exist",
                                     table_id);
          goto done;
      }

      lfe = fib_table_entry_path_add2(fib_index,
				      &pfx,
				      FIB_SOURCE_CLI,
				      FIB_ENTRY_FLAG_NONE,
				      rpaths);

      if (FIB_NODE_INDEX_INVALID == lfe)
      {
          error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d",
                                     format_mpls_unicast_label, local_label,
                                     format_mpls_eos_bit, eos,
                                     table_id);
          goto done;
      }
  }

done:
  return error;
}

VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
  .path = "mpls local-label",
  .function = vnet_mpls_local_label,
  .short_help = "Create/Delete MPL local labels",
};

int mpls_fib_reset_labels (u32 fib_id)
{
  u64 key; 
  u32 value;
  show_mpls_fib_t *records = 0;
  show_mpls_fib_t *s;
  mpls_main_t * mm = &mpls_main;
  ip4_main_t * im = &ip4_main;
  u32 fib_index;
  uword *p;

  p = hash_get (im->fib_index_by_table_id, fib_id);
  if (! p)
    return VNET_API_ERROR_NO_SUCH_FIB;

  fib_index = p[0];

  hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest, 
  ({
    if (fib_index == (u32)(key>>32)) {
        vec_add2 (records, s, 1);
        s->dest = (u32)(key & 0xFFFFFFFF);
        s->entry_index = (u32) value;
    }
  }));

  vec_foreach (s, records)
    {
      key = ((u64)fib_index<<32) | ((u64) s->dest);
      hash_unset (mm->mpls_encap_by_fib_and_dest, key);
      pool_put_index (mm->encaps, s->entry_index);
    }
                
  vec_free(records);
  return 0;
}

static clib_error_t * mpls_init (vlib_main_t * vm)
{
  mpls_main_t * mm = &mpls_main;
  clib_error_t * error;

  mm->vlib_main = vm;
  mm->vnet_main = vnet_get_main();

  if ((error = vlib_call_init_function (vm, ip_main_init)))
    return error;

  mm->mpls_encap_by_fib_and_dest = hash_create (0, sizeof (uword));

  return vlib_call_init_function (vm, mpls_input_init);
}

VLIB_INIT_FUNCTION (mpls_init);

mpls_main_t * mpls_get_main (vlib_main_t * vm)
{
  vlib_call_init_function (vm, mpls_init);
  return &mpls_main;
}