nsh.c revision 63bad81f
1/*
2 * nsh.c - nsh mapping
3 *
4 * Copyright (c) 2013 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/plugin/plugin.h>
20#include <nsh/nsh.h>
21#include <vnet/gre/gre.h>
22#include <vnet/vxlan/vxlan.h>
23#include <vnet/vxlan-gpe/vxlan_gpe.h>
24#include <vnet/l2/l2_classify.h>
25
26#include <vlibapi/api.h>
27#include <vlibmemory/api.h>
28#include <vlibsocket/api.h>
29
30/* define message IDs */
31#include <vpp-api/nsh_msg_enum.h>
32
33/* define message structures */
34#define vl_typedefs
35#include <vpp-api/nsh_all_api_h.h>
36#undef vl_typedefs
37
38/* define generated endian-swappers */
39#define vl_endianfun
40#include <vpp-api/nsh_all_api_h.h>
41#undef vl_endianfun
42
43/* instantiate all the print functions we know about */
44#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45#define vl_printfun
46#include <vpp-api/nsh_all_api_h.h>
47#undef vl_printfun
48
49/* Get the API version number */
50#define vl_api_version(n,v) static u32 api_version=(v);
51#include <vpp-api/nsh_all_api_h.h>
52#undef vl_api_version
53
54#define vl_msg_name_crc_list
55#include <vpp-api/nsh_all_api_h.h>
56#undef vl_msg_name_crc_list
57
58/*
59 * A handy macro to set up a message reply.
60 * Assumes that the following variables are available:
61 * mp - pointer to request message
62 * rmp - pointer to reply message type
63 * rv - return value
64 */
65
66#define REPLY_MACRO(t)                                          \
67  do {								\
68    unix_shared_memory_queue_t * q =                            \
69      vl_api_client_index_to_input_queue (mp->client_index);	\
70    if (!q)                                                     \
71      return;							\
72                                                                \
73    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
74    rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base);               \
75    rmp->context = mp->context;                                 \
76    rmp->retval = ntohl(rv);                                    \
77                                                                \
78    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
79  } while(0);
80
81#define REPLY_MACRO2(t, body)                                   \
82  do {                                                          \
83    unix_shared_memory_queue_t * q;                             \
84    rv = vl_msg_api_pd_handler (mp, rv);                        \
85    q = vl_api_client_index_to_input_queue (mp->client_index);  \
86    if (!q)                                                     \
87        return;                                                 \
88                                                                \
89    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
90    rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base);               \
91    rmp->context = mp->context;                                 \
92    rmp->retval = ntohl(rv);                                    \
93    do {body;} while (0);                                       \
94    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
95  } while(0);
96
97#define FINISH                                  \
98    vec_add1 (s, 0);                            \
99    vl_print (handle, (char *)s);               \
100    vec_free (s);                               \
101    return handle;
102
103/* List of message types that this plugin understands */
104
105#define foreach_nsh_plugin_api_msg		\
106  _(NSH_ADD_DEL_ENTRY, nsh_add_del_entry)	\
107  _(NSH_ENTRY_DUMP, nsh_entry_dump)             \
108  _(NSH_ADD_DEL_MAP, nsh_add_del_map)           \
109  _(NSH_MAP_DUMP, nsh_map_dump)
110
111/* *INDENT-OFF* */
112VLIB_PLUGIN_REGISTER () = {
113};
114/* *INDENT-ON* */
115
116 /* Uses network order's class and type to register */
117int
118nsh_md2_register_option (u16 class,
119                      u8 type,
120                      u8 option_size,
121                      int add_options (u8 * opt,
122                                       u8 * opt_size),
123                      int options(vlib_buffer_t * b,
124                                  nsh_tlv_header_t * opt),
125                      int swap_options (vlib_buffer_t * b,
126				        nsh_tlv_header_t * old_opt,
127		                        nsh_tlv_header_t * new_opt),
128                      int pop_options (vlib_buffer_t * b,
129                                       nsh_tlv_header_t * opt),
130                      u8 * trace (u8 * s,
131                                  nsh_tlv_header_t * opt))
132{
133  nsh_main_t *nm = &nsh_main;
134  nsh_option_map_by_key_t key, *key_copy;
135  uword *p;
136  nsh_option_map_t *nsh_option;
137
138  key.class = class;
139  key.type = type;
140  key.pad = 0;
141
142  p = hash_get_mem(nm->nsh_option_map_by_key, &key);
143  /* Already registered */
144  if (p != 0)
145    {
146      return (-1);
147    }
148
149  pool_get_aligned (nm->nsh_option_mappings, nsh_option, CLIB_CACHE_LINE_BYTES);
150  memset (nsh_option, 0, sizeof (*nsh_option));
151  nsh_option->option_id = nsh_option - nm->nsh_option_mappings;
152
153  key_copy = clib_mem_alloc (sizeof (*key_copy));
154  clib_memcpy (key_copy, &key, sizeof (*key_copy));
155  hash_set_mem (nm->nsh_option_map_by_key, key_copy,
156	        nsh_option - nm->nsh_option_mappings);
157
158  if(option_size > (MAX_NSH_OPTION_LEN + sizeof(nsh_tlv_header_t)) )
159    {
160      return (-1);
161    }
162  nm->options_size[nsh_option->option_id] = option_size;
163  nm->add_options[nsh_option->option_id] = add_options;
164  nm->options[nsh_option->option_id] = options;
165  nm->swap_options[nsh_option->option_id] = swap_options;
166  nm->pop_options[nsh_option->option_id] = pop_options;
167  nm->trace[nsh_option->option_id] = trace;
168
169  return (0);
170}
171
172/* Uses network order's class and type to lookup */
173nsh_option_map_t *
174nsh_md2_lookup_option (u16 class, u8 type)
175{
176  nsh_main_t *nm = &nsh_main;
177  nsh_option_map_by_key_t key;
178  uword *p;
179
180  key.class = class;
181  key.type = type;
182  key.pad = 0;
183
184  p = hash_get_mem(nm->nsh_option_map_by_key, &key);
185  /* not registered */
186  if (p == 0)
187    {
188      return NULL;
189    }
190
191  return pool_elt_at_index(nm->nsh_option_mappings, p[0]);
192
193}
194
195/* Uses network order's class and type to unregister */
196int
197nsh_md2_unregister_option (u16 class,
198			u8 type,
199			int options(vlib_buffer_t * b,
200				   nsh_tlv_header_t * opt),
201			u8 * trace (u8 * s,
202				   nsh_tlv_header_t * opt))
203{
204  nsh_main_t *nm = &nsh_main;
205  nsh_option_map_by_key_t key, *key_copy;
206  uword *p;
207  hash_pair_t *hp;
208  nsh_option_map_t *nsh_option;
209
210  key.class = class;
211  key.type = type;
212  key.pad = 0;
213
214  p = hash_get_mem(nm->nsh_option_map_by_key, &key);
215  /* not registered */
216  if (p == 0)
217    {
218      return (-1);
219    }
220
221  nsh_option = pool_elt_at_index(nm->nsh_option_mappings, p[0]);
222  nm->options[nsh_option->option_id] = NULL;
223  nm->add_options[nsh_option->option_id] = NULL;
224  nm->pop_options[nsh_option->option_id] = NULL;
225  nm->trace[nsh_option->option_id] = NULL;
226
227  hp = hash_get_pair (nm->nsh_option_map_by_key, &key);
228  key_copy = (void *)(hp->key);
229  hash_unset_mem (nm->nsh_option_map_by_key, &key_copy);
230  clib_mem_free (key_copy);
231
232  pool_put (nm->nsh_option_mappings, nsh_option);
233
234  return (0);
235}
236
237/* format from network order */
238u8 * format_nsh_header (u8 * s, va_list * args)
239{
240  nsh_main_t *nm = &nsh_main;
241  nsh_md2_data_t *opt0;
242  nsh_md2_data_t *limit0;
243  nsh_option_map_t *nsh_option;
244  u8 option_len = 0;
245
246  u8 * header = va_arg (*args, u8 *);
247  nsh_base_header_t * nsh_base = (nsh_base_header_t *) header ;
248  nsh_md1_data_t * nsh_md1 = (nsh_md1_data_t *)(nsh_base + 1);
249  nsh_md2_data_t * nsh_md2 = (nsh_md2_data_t *)(nsh_base + 1);
250  opt0 = (nsh_md2_data_t *)nsh_md2;
251  limit0 = (nsh_md2_data_t *) ((u8 *) nsh_md2 +
252                              (nsh_base->length*4-sizeof(nsh_base_header_t)) );
253
254  s = format (s, "nsh ver %d ", (nsh_base->ver_o_c>>6));
255  if (nsh_base->ver_o_c & NSH_O_BIT)
256    s = format (s, "O-set ");
257
258  if (nsh_base->ver_o_c & NSH_C_BIT)
259    s = format (s, "C-set ");
260
261  s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
262	      nsh_base->length, nsh_base->length * 4,
263	      nsh_base->md_type, nsh_base->next_protocol);
264
265  s = format (s, "  service path %d service index %d\n",
266        (clib_net_to_host_u32(nsh_base->nsp_nsi)>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
267	 clib_net_to_host_u32(nsh_base->nsp_nsi) & NSH_NSI_MASK);
268
269  if (nsh_base->md_type == 1 )
270    {
271      s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
272		  clib_net_to_host_u32(nsh_md1->c1),
273		  clib_net_to_host_u32(nsh_md1->c2),
274		  clib_net_to_host_u32(nsh_md1->c3),
275		  clib_net_to_host_u32(nsh_md1->c4) );
276    }
277  else if (nsh_base->md_type == 2 )
278    {
279      s = format (s, "  Supported TLVs: \n");
280
281      /* Scan the set of variable metadata, network order */
282      while (opt0 < limit0)
283        {
284          nsh_option = nsh_md2_lookup_option(opt0->class, opt0->type);
285          if( nsh_option != NULL)
286    	    {
287    	      if (nm->trace[nsh_option->option_id] != NULL)
288    	        {
289    	          s = (*nm->trace[nsh_option->option_id]) (s, opt0);
290    	        }
291    	      else
292    	        {
293    	          s = format (s, "\n    untraced option %d length %d", opt0->type,
294    			  opt0->length);
295    	        }
296    	    }
297          else
298            {
299              s = format (s, "\n    unrecognized option %d length %d", opt0->type,
300                          opt0->length);
301            }
302
303          /* round to 4-byte */
304          option_len = ( (opt0->length+3)>>2 ) << 2;
305          opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + sizeof (nsh_md2_data_t) + option_len);
306        }
307    }
308
309  return s;
310}
311
312static u8 * format_nsh_action (u8 * s, va_list * args)
313{
314  u32 nsh_action = va_arg (*args, u32);
315
316  switch (nsh_action)
317    {
318    case NSH_ACTION_SWAP:
319      return format (s, "swap");
320    case NSH_ACTION_PUSH:
321      return format (s, "push");
322    case NSH_ACTION_POP:
323      return format (s, "pop");
324    default:
325      return format (s, "unknown %d", nsh_action);
326    }
327  return s;
328}
329
330u8 * format_nsh_map (u8 * s, va_list * args)
331{
332  nsh_map_t * map = va_arg (*args, nsh_map_t *);
333
334  s = format (s, "nsh entry nsp: %d nsi: %d ",
335              (map->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
336              map->nsp_nsi & NSH_NSI_MASK);
337  s = format (s, "maps to nsp: %d nsi: %d ",
338              (map->mapped_nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
339              map->mapped_nsp_nsi & NSH_NSI_MASK);
340
341  s = format (s, " nsh_action %U\n", format_nsh_action, map->nsh_action);
342
343  switch (map->next_node)
344    {
345    case NSH_NODE_NEXT_ENCAP_GRE4:
346      {
347	s = format (s, "encapped by GRE4 intf: %d", map->sw_if_index);
348	break;
349      }
350    case NSH_NODE_NEXT_ENCAP_GRE6:
351      {
352	s = format (s, "encapped by GRE6 intf: %d", map->sw_if_index);
353	break;
354      }
355    case NSH_NODE_NEXT_ENCAP_VXLANGPE:
356      {
357	s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
358	break;
359      }
360    case NSH_NODE_NEXT_ENCAP_VXLAN4:
361      {
362	s = format (s, "encapped by VXLAN4 intf: %d", map->sw_if_index);
363	break;
364      }
365    case NSH_NODE_NEXT_ENCAP_VXLAN6:
366      {
367	s = format (s, "encapped by VXLAN6 intf: %d", map->sw_if_index);
368	break;
369      }
370    case NSH_NODE_NEXT_DECAP_ETH_INPUT:
371      {
372	s = format (s, "encap-none");
373	break;
374      }
375    case NSH_NODE_NEXT_ENCAP_LISP_GPE:
376      {
377       s = format (s, "encapped by LISP GPE intf: %d", map->sw_if_index);
378       break;
379      }
380    default:
381      s = format (s, "only GRE and VXLANGPE support in this rev");
382    }
383
384  return s;
385}
386
387u8 * format_nsh_node_map_trace (u8 * s, va_list * args)
388{
389  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
390  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
391  nsh_input_trace_t * t
392    = va_arg (*args, nsh_input_trace_t *);
393
394  s = format (s, "\n  %U", format_nsh_header, &(t->trace_data) );
395
396  return s;
397}
398
399/**
400 * @brief Naming for NSH tunnel
401 *
402 * @param *s formatting string
403 * @param *args
404 *
405 * @return *s formatted string
406 *
407 */
408static u8 * format_nsh_name (u8 * s, va_list * args)
409{
410  u32 dev_instance = va_arg (*args, u32);
411  return format (s, "nsh_tunnel%d", dev_instance);
412}
413
414/**
415 * @brief CLI function for NSH admin up/down
416 *
417 * @param *vnm
418 * @param nsh_hw_if
419 * @param flag
420 *
421 * @return *rc
422 *
423 */
424static clib_error_t *
425nsh_interface_admin_up_down (vnet_main_t * vnm, u32 nsh_hw_if, u32 flags)
426{
427  if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
428    vnet_hw_interface_set_flags (vnm, nsh_hw_if, VNET_HW_INTERFACE_FLAG_LINK_UP);
429  else
430    vnet_hw_interface_set_flags (vnm, nsh_hw_if, 0);
431
432  return 0;
433}
434
435static uword dummy_interface_tx (vlib_main_t * vm,
436                                 vlib_node_runtime_t * node,
437                                 vlib_frame_t * frame)
438{
439  clib_warning ("you shouldn't be here, leaking buffers...");
440  return frame->n_vectors;
441}
442
443VNET_DEVICE_CLASS (nsh_device_class,static) = {
444  .name = "NSH",
445  .format_device_name = format_nsh_name,
446  .tx_function = dummy_interface_tx,
447  .admin_up_down_function = nsh_interface_admin_up_down,
448};
449
450/**
451 * @brief Formatting function for tracing VXLAN GPE with length
452 *
453 * @param *s
454 * @param *args
455 *
456 * @return *s
457 *
458 */
459static u8 * format_nsh_tunnel_with_length (u8 * s, va_list * args)
460{
461  u32 dev_instance = va_arg (*args, u32);
462  s = format (s, "unimplemented dev %u", dev_instance);
463  return s;
464}
465
466VNET_HW_INTERFACE_CLASS (nsh_hw_class) = {
467  .name = "NSH",
468  .format_header = format_nsh_tunnel_with_length,
469  .build_rewrite = default_build_rewrite,
470  .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
471};
472
473
474/**
475 * Action function to add or del an nsh map.
476 * Shared by both CLI and binary API
477 **/
478
479int nsh_add_del_map (nsh_add_del_map_args_t *a, u32 * map_indexp)
480{
481  nsh_main_t * nm = &nsh_main;
482  vnet_main_t * vnm = nm->vnet_main;
483  nsh_map_t *map = 0;
484  u32 key, *key_copy;
485  uword * entry;
486  hash_pair_t *hp;
487  u32 map_index = ~0;
488  vnet_hw_interface_t * hi;
489  u32 nsh_hw_if = ~0;
490  u32 nsh_sw_if = ~0;
491
492  /* net order, so data plane could use nsh header to lookup directly */
493  key = clib_host_to_net_u32(a->map.nsp_nsi);
494
495  entry = hash_get_mem (nm->nsh_mapping_by_key, &key);
496
497  if (a->is_add)
498    {
499      /* adding an entry, must not already exist */
500      if (entry)
501        return -1; //TODO API_ERROR_INVALID_VALUE;
502
503      pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES);
504      memset (map, 0, sizeof (*map));
505
506      /* copy from arg structure */
507      map->nsp_nsi = a->map.nsp_nsi;
508      map->mapped_nsp_nsi = a->map.mapped_nsp_nsi;
509      map->nsh_action = a->map.nsh_action;
510      map->sw_if_index = a->map.sw_if_index;
511      map->rx_sw_if_index = a->map.rx_sw_if_index;
512      map->next_node = a->map.next_node;
513
514
515      key_copy = clib_mem_alloc (sizeof (*key_copy));
516      clib_memcpy (key_copy, &key, sizeof (*key_copy));
517
518      hash_set_mem (nm->nsh_mapping_by_key, key_copy,
519                    map - nm->nsh_mappings);
520      map_index = map - nm->nsh_mappings;
521
522      if (vec_len (nm->free_nsh_tunnel_hw_if_indices) > 0)
523        {
524          nsh_hw_if = nm->free_nsh_tunnel_hw_if_indices
525            [vec_len (nm->free_nsh_tunnel_hw_if_indices)-1];
526          _vec_len (nm->free_nsh_tunnel_hw_if_indices) -= 1;
527
528          hi = vnet_get_hw_interface (vnm, nsh_hw_if);
529          hi->dev_instance = map_index;
530          hi->hw_instance = hi->dev_instance;
531        }
532      else
533        {
534          nsh_hw_if = vnet_register_interface
535            (vnm, nsh_device_class.index, map_index, nsh_hw_class.index, map_index);
536          hi = vnet_get_hw_interface (vnm, nsh_hw_if);
537          hi->output_node_index = nsh_aware_vnf_proxy_node.index;
538        }
539
540      map->nsh_hw_if = nsh_hw_if;
541      map->nsh_sw_if = nsh_sw_if = hi->sw_if_index;
542      vec_validate_init_empty (nm->tunnel_index_by_sw_if_index, nsh_sw_if, ~0);
543      nm->tunnel_index_by_sw_if_index[nsh_sw_if] = key;
544
545      vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
546                                   VNET_SW_INTERFACE_FLAG_ADMIN_UP);
547    }
548  else
549    {
550      if (!entry)
551	return -2 ; //TODO API_ERROR_NO_SUCH_ENTRY;
552
553      map = pool_elt_at_index (nm->nsh_mappings, entry[0]);
554
555      vnet_sw_interface_set_flags (vnm, map->nsh_sw_if,
556				   VNET_SW_INTERFACE_FLAG_ADMIN_DOWN);
557      vec_add1 (nm->free_nsh_tunnel_hw_if_indices, map->nsh_sw_if);
558      nm->tunnel_index_by_sw_if_index[map->nsh_sw_if] = ~0;
559
560      hp = hash_get_pair (nm->nsh_mapping_by_key, &key);
561      key_copy = (void *)(hp->key);
562      hash_unset_mem (nm->nsh_mapping_by_key, &key);
563      clib_mem_free (key_copy);
564
565      pool_put (nm->nsh_mappings, map);
566    }
567
568  if (map_indexp)
569      *map_indexp = map_index;
570
571  return 0;
572}
573
574/**
575 * Action function to add or del an nsh-proxy-session.
576 * Shared by both CLI and binary API
577 **/
578
579int nsh_add_del_proxy_session (nsh_add_del_map_args_t *a)
580{
581  nsh_main_t * nm = &nsh_main;
582  nsh_proxy_session_t *proxy = 0;
583  nsh_proxy_session_by_key_t key, *key_copy;
584  uword * entry;
585  hash_pair_t *hp;
586  u32 nsp = 0, nsi = 0;
587
588  memset (&key, 0, sizeof (key));
589  key.transport_type = a->map.next_node;
590  key.transport_index = a->map.sw_if_index;
591
592  entry = hash_get_mem (nm->nsh_proxy_session_by_key, &key);
593
594  if (a->is_add)
595    {
596      /* adding an entry, must not already exist */
597      if (entry)
598        return -1; //TODO API_ERROR_INVALID_VALUE;
599
600      pool_get_aligned (nm->nsh_proxy_sessions, proxy, CLIB_CACHE_LINE_BYTES);
601      memset (proxy, 0, sizeof (*proxy));
602
603      /* Nsi needs to minus 1 within NSH-Proxy */
604      nsp = (a->map.nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK;
605      nsi = a->map.nsp_nsi & NSH_NSI_MASK;
606      if (nsi == 0 )
607	return -1;
608
609      nsi = nsi -1;
610      /* net order, so could use it to lookup nsh map table directly */
611      proxy->nsp_nsi = clib_host_to_net_u32((nsp<< NSH_NSP_SHIFT) | nsi);
612
613      key_copy = clib_mem_alloc (sizeof (*key_copy));
614      clib_memcpy (key_copy, &key, sizeof (*key_copy));
615
616      hash_set_mem (nm->nsh_proxy_session_by_key, key_copy,
617		    proxy - nm->nsh_proxy_sessions);
618    }
619  else
620    {
621      if (!entry)
622	return -2 ; //TODO API_ERROR_NO_SUCH_ENTRY;
623
624      proxy = pool_elt_at_index (nm->nsh_proxy_sessions, entry[0]);
625      hp = hash_get_pair (nm->nsh_proxy_session_by_key, &key);
626      key_copy = (void *)(hp->key);
627      hash_unset_mem (nm->nsh_proxy_session_by_key, &key);
628      clib_mem_free (key_copy);
629
630      pool_put (nm->nsh_proxy_sessions, proxy);
631    }
632
633  return 0;
634}
635
636/**
637 * CLI command for NSH map
638 */
639
640static uword unformat_nsh_action (unformat_input_t * input, va_list * args)
641{
642  u32 * result = va_arg (*args, u32 *);
643  u32 tmp;
644
645  if (unformat (input, "swap"))
646    *result = NSH_ACTION_SWAP;
647  else if (unformat (input, "push"))
648    *result = NSH_ACTION_PUSH;
649  else if (unformat (input, "pop"))
650    *result = NSH_ACTION_POP;
651  else if (unformat (input, "%d", &tmp))
652    *result = tmp;
653  else
654    return 0;
655
656  return 1;
657}
658
659static clib_error_t *
660nsh_add_del_map_command_fn (vlib_main_t * vm,
661			    unformat_input_t * input,
662			    vlib_cli_command_t * cmd)
663{
664  unformat_input_t _line_input, * line_input = &_line_input;
665  u8 is_add = 1;
666  u32 nsp, nsi, mapped_nsp, mapped_nsi, nsh_action;
667  int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
668  int nsh_action_set = 0;
669  u32 next_node = ~0;
670  u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
671  u32 rx_sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
672  nsh_add_del_map_args_t _a, * a = &_a;
673  u32 map_index;
674  int rv;
675
676  /* Get a line of input. */
677  if (! unformat_user (input, unformat_line_input, line_input))
678    return 0;
679
680  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
681    if (unformat (line_input, "del"))
682      is_add = 0;
683    else if (unformat (line_input, "nsp %d", &nsp))
684      nsp_set = 1;
685    else if (unformat (line_input, "nsi %d", &nsi))
686      nsi_set = 1;
687    else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
688      mapped_nsp_set = 1;
689    else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
690      mapped_nsi_set = 1;
691    else if (unformat (line_input, "nsh_action %U", unformat_nsh_action,
692                       &nsh_action))
693      nsh_action_set = 1;
694    else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
695      next_node = NSH_NODE_NEXT_ENCAP_GRE4;
696    else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
697      next_node = NSH_NODE_NEXT_ENCAP_GRE6;
698    else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
699      next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
700    else if (unformat (line_input, "encap-lisp-gpe-intf %d", &sw_if_index))
701      next_node = NSH_NODE_NEXT_ENCAP_LISP_GPE;
702    else if (unformat (line_input, "encap-vxlan4-intf %d", &sw_if_index))
703      next_node = NSH_NODE_NEXT_ENCAP_VXLAN4;
704    else if (unformat (line_input, "encap-vxlan6-intf %d", &sw_if_index))
705      next_node = NSH_NODE_NEXT_ENCAP_VXLAN6;
706    else if (unformat (line_input, "encap-none %d %d", &sw_if_index, &rx_sw_if_index))
707      next_node = NSH_NODE_NEXT_DECAP_ETH_INPUT;
708    else
709      return clib_error_return (0, "parse error: '%U'",
710                                format_unformat_error, line_input);
711  }
712
713  unformat_free (line_input);
714
715  if (nsp_set == 0 || nsi_set == 0)
716    return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
717
718  if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
719    return clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
720
721  if (nsh_action_set == 0 )
722    return clib_error_return (0, "nsh_action required: swap|push|pop.");
723
724  if (next_node == ~0)
725    return clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> | encap-none <tx_sw_if_index> <rx_sw_if_index>]");
726
727  memset (a, 0, sizeof (*a));
728
729  /* set args structure */
730  a->is_add = is_add;
731  a->map.nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
732  a->map.mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
733  a->map.nsh_action = nsh_action;
734  a->map.sw_if_index = sw_if_index;
735  a->map.rx_sw_if_index = rx_sw_if_index;
736  a->map.next_node = next_node;
737
738  rv = nsh_add_del_map(a, &map_index);
739
740  switch(rv)
741    {
742    case 0:
743      break;
744    case -1: //TODO API_ERROR_INVALID_VALUE:
745      return clib_error_return (0, "mapping already exists. Remove it first.");
746
747    case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
748      return clib_error_return (0, "mapping does not exist.");
749
750    default:
751      return clib_error_return
752        (0, "nsh_add_del_map returned %d", rv);
753    }
754
755  if((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4)
756      | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6))
757    {
758      rv = nsh_add_del_proxy_session(a);
759
760      switch(rv)
761        {
762        case 0:
763          break;
764        case -1: //TODO API_ERROR_INVALID_VALUE:
765          return clib_error_return (0, "nsh-proxy-session already exists. Remove it first.");
766
767        case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
768          return clib_error_return (0, "nsh-proxy-session does not exist.");
769
770        default:
771          return clib_error_return
772            (0, "nsh_add_del_proxy_session() returned %d", rv);
773        }
774    }
775
776  return 0;
777}
778
779VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
780  .path = "create nsh map",
781  .short_help =
782  "create nsh map nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> nsh_action [swap|push|pop] "
783  "[encap-gre4-intf <nn> | encap-gre4-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> "
784  " encap-vxlan4-intf <nn> | encap-vxlan6-intf <nn> | encap-none]\n",
785  .function = nsh_add_del_map_command_fn,
786};
787
788/** API message handler */
789static void vl_api_nsh_add_del_map_t_handler
790(vl_api_nsh_add_del_map_t * mp)
791{
792  vl_api_nsh_add_del_map_reply_t * rmp;
793  nsh_main_t * nm = &nsh_main;
794  int rv;
795  nsh_add_del_map_args_t _a, *a = &_a;
796  u32 map_index = ~0;
797
798  a->is_add = mp->is_add;
799  a->map.nsp_nsi = ntohl(mp->nsp_nsi);
800  a->map.mapped_nsp_nsi = ntohl(mp->mapped_nsp_nsi);
801  a->map.nsh_action = ntohl(mp->nsh_action);
802  a->map.sw_if_index = ntohl(mp->sw_if_index);
803  a->map.rx_sw_if_index = ntohl(mp->rx_sw_if_index);
804  a->map.next_node = ntohl(mp->next_node);
805
806  rv = nsh_add_del_map (a, &map_index);
807
808  if((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4)
809      | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6))
810    {
811      rv = nsh_add_del_proxy_session(a);
812    }
813
814  REPLY_MACRO2(VL_API_NSH_ADD_DEL_MAP_REPLY,
815  ({
816    rmp->map_index = htonl (map_index);
817  }));
818}
819
820/**
821 * CLI command for showing the mapping between NSH entries
822 */
823static clib_error_t *
824show_nsh_map_command_fn (vlib_main_t * vm,
825			 unformat_input_t * input,
826			 vlib_cli_command_t * cmd)
827{
828  nsh_main_t * nm = &nsh_main;
829  nsh_map_t * map;
830
831  if (pool_elts (nm->nsh_mappings) == 0)
832    vlib_cli_output (vm, "No nsh maps configured.");
833
834  pool_foreach (map, nm->nsh_mappings,
835		({
836		  vlib_cli_output (vm, "%U", format_nsh_map, map);
837		}));
838
839  return 0;
840}
841
842VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
843  .path = "show nsh map",
844  .function = show_nsh_map_command_fn,
845};
846
847int nsh_header_rewrite(nsh_entry_t *nsh_entry)
848{
849  u8 *rw = 0;
850  int len = 0;
851  nsh_base_header_t * nsh_base;
852  nsh_md1_data_t * nsh_md1;
853  nsh_main_t *nm = &nsh_main;
854  nsh_md2_data_t *opt0;
855  nsh_md2_data_t *limit0;
856  nsh_md2_data_t *nsh_md2;
857  nsh_option_map_t _nsh_option, *nsh_option=&_nsh_option;
858  u8 old_option_size = 0;
859  u8 new_option_size = 0;
860
861  vec_free(nsh_entry->rewrite);
862  if (nsh_entry->nsh_base.md_type == 1)
863    {
864      len = sizeof(nsh_base_header_t) + sizeof(nsh_md1_data_t);
865    }
866  else if (nsh_entry->nsh_base.md_type == 2)
867    {
868      /* set to maxim, maybe dataplane will add more TLVs */
869      len = MAX_NSH_HEADER_LEN;
870    }
871  vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
872  memset (rw, 0, len);
873
874  nsh_base = (nsh_base_header_t *) rw;
875  nsh_base->ver_o_c = nsh_entry->nsh_base.ver_o_c;
876  nsh_base->md_type = nsh_entry->nsh_base.md_type;
877  nsh_base->next_protocol = nsh_entry->nsh_base.next_protocol;
878  nsh_base->nsp_nsi = clib_host_to_net_u32(nsh_entry->nsh_base.nsp_nsi);
879
880  if (nsh_base->md_type == 1)
881    {
882      nsh_md1 =(nsh_md1_data_t *)(rw + sizeof(nsh_base_header_t));
883      nsh_md1->c1 = clib_host_to_net_u32(nsh_entry->md.md1_data.c1);
884      nsh_md1->c2 = clib_host_to_net_u32(nsh_entry->md.md1_data.c2);
885      nsh_md1->c3 = clib_host_to_net_u32(nsh_entry->md.md1_data.c3);
886      nsh_md1->c4 = clib_host_to_net_u32(nsh_entry->md.md1_data.c4);
887      nsh_entry->rewrite_size = 24;
888    }
889  else if (nsh_base->md_type == 2)
890    {
891      opt0 = (nsh_md2_data_t *)(nsh_entry->tlvs_data);
892      limit0 = (nsh_md2_data_t *) ((u8 *) opt0 + nsh_entry->tlvs_len);
893
894      nsh_md2 = (nsh_md2_data_t *)(rw + sizeof(nsh_base_header_t));
895      nsh_entry->rewrite_size = sizeof(nsh_base_header_t);
896
897      while(opt0 < limit0)
898	{
899          old_option_size = sizeof (nsh_md2_data_t) + opt0->length;
900          /* round to 4-byte */
901          old_option_size = ( (old_option_size+3)>>2 ) << 2;
902
903          nsh_option = nsh_md2_lookup_option(opt0->class, opt0->type);
904          if( nsh_option == NULL)
905            {
906              goto next_tlv_md2;
907            }
908
909          if(nm->add_options[nsh_option->option_id] != NULL)
910            {
911              if (0 != nm->add_options[nsh_option->option_id] (
912        	  (u8 *)nsh_md2, &new_option_size ))
913                {
914                  goto next_tlv_md2;
915                }
916
917              /* round to 4-byte */
918              new_option_size = ( (new_option_size+3)>>2 ) << 2;
919
920              nsh_entry->rewrite_size += new_option_size;
921              nsh_md2 = (nsh_md2_data_t *) (((u8 *) nsh_md2) + new_option_size);
922              opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + old_option_size);
923            }
924          else
925            {
926next_tlv_md2:
927              opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + old_option_size);
928            }
929
930	}
931    }
932
933  nsh_entry->rewrite = rw;
934  nsh_base->length = nsh_entry->rewrite_size >> 2;
935
936  return 0;
937}
938
939
940/**
941 * Action function for adding an NSH entry
942 * nsh_add_del_entry_args_t *a: host order
943 */
944
945int nsh_add_del_entry (nsh_add_del_entry_args_t *a, u32 * entry_indexp)
946{
947  nsh_main_t * nm = &nsh_main;
948  nsh_entry_t *nsh_entry = 0;
949  u32 key, *key_copy;
950  uword * entry_id;
951  hash_pair_t *hp;
952  u32 entry_index = ~0;
953  u8 tlvs_len = 0;
954  u8 * data = 0;
955
956  /* host order, because nsh map table stores nsp_nsi in host order */
957  key = a->nsh_entry.nsh_base.nsp_nsi;
958
959  entry_id = hash_get_mem (nm->nsh_entry_by_key, &key);
960
961  if (a->is_add)
962    {
963      /* adding an entry, must not already exist */
964      if (entry_id)
965        return -1; // TODO VNET_API_ERROR_INVALID_VALUE;
966
967      pool_get_aligned (nm->nsh_entries, nsh_entry, CLIB_CACHE_LINE_BYTES);
968      memset (nsh_entry, 0, sizeof (*nsh_entry));
969
970      /* copy from arg structure */
971#define _(x) nsh_entry->nsh_base.x = a->nsh_entry.nsh_base.x;
972      foreach_copy_nsh_base_hdr_field;
973#undef _
974
975      if (a->nsh_entry.nsh_base.md_type == 1)
976        {
977          nsh_entry->md.md1_data.c1 = a->nsh_entry.md.md1_data.c1;
978          nsh_entry->md.md1_data.c2 = a->nsh_entry.md.md1_data.c2;
979          nsh_entry->md.md1_data.c3 = a->nsh_entry.md.md1_data.c3;
980          nsh_entry->md.md1_data.c4 = a->nsh_entry.md.md1_data.c4;
981        }
982      else if (a->nsh_entry.nsh_base.md_type == 2)
983        {
984	  vec_free(nsh_entry->tlvs_data);
985	  tlvs_len = a->nsh_entry.tlvs_len;
986          vec_validate_aligned(data, tlvs_len-1, CLIB_CACHE_LINE_BYTES);
987
988          clib_memcpy(data, a->nsh_entry.tlvs_data, tlvs_len);
989	  nsh_entry->tlvs_data = data;
990	  nsh_entry->tlvs_len = tlvs_len;
991	  vec_free(a->nsh_entry.tlvs_data);
992        }
993
994      nsh_header_rewrite(nsh_entry);
995
996      key_copy = clib_mem_alloc (sizeof (*key_copy));
997      clib_memcpy (key_copy, &key, sizeof (*key_copy));
998
999      hash_set_mem (nm->nsh_entry_by_key, key_copy,
1000                    nsh_entry - nm->nsh_entries);
1001      entry_index = nsh_entry - nm->nsh_entries;
1002    }
1003  else
1004    {
1005      if (!entry_id)
1006	return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
1007
1008      nsh_entry = pool_elt_at_index (nm->nsh_entries, entry_id[0]);
1009      hp = hash_get_pair (nm->nsh_entry_by_key, &key);
1010      key_copy = (void *)(hp->key);
1011      hash_unset_mem (nm->nsh_entry_by_key, &key);
1012      clib_mem_free (key_copy);
1013
1014      vec_free (nsh_entry->tlvs_data);
1015      vec_free (nsh_entry->rewrite);
1016      pool_put (nm->nsh_entries, nsh_entry);
1017    }
1018
1019  if (entry_indexp)
1020      *entry_indexp = entry_index;
1021
1022  return 0;
1023}
1024
1025
1026/**
1027 * CLI command for adding NSH entry
1028 */
1029
1030static clib_error_t *
1031nsh_add_del_entry_command_fn (vlib_main_t * vm,
1032			      unformat_input_t * input,
1033			      vlib_cli_command_t * cmd)
1034{
1035  unformat_input_t _line_input, * line_input = &_line_input;
1036  u8 is_add = 1;
1037  u8 ver_o_c = 0;
1038  u8 length = 0;
1039  u8 md_type = 0;
1040  u8 next_protocol = 1; /* default: ip4 */
1041  u32 nsp;
1042  u8 nsp_set = 0;
1043  u32 nsi;
1044  u8 nsi_set = 0;
1045  u32 nsp_nsi;
1046  u32 c1 = 0;
1047  u32 c2 = 0;
1048  u32 c3 = 0;
1049  u32 c4 = 0;
1050  u8 * data = 0;
1051  nsh_tlv_header_t tlv_header;
1052  u8 cur_len = 0, tlvs_len = 0;
1053  u8 * current;
1054  nsh_main_t *nm = &nsh_main;
1055  nsh_option_map_t _nsh_option, *nsh_option=&_nsh_option;
1056  u8 option_size = 0;
1057  u32 tmp;
1058  int rv;
1059  u32 entry_index;
1060  nsh_add_del_entry_args_t _a, * a = &_a;
1061  u8 has_ioam_trace_option = 0;
1062
1063  /* Get a line of input. */
1064  if (! unformat_user (input, unformat_line_input, line_input))
1065    return 0;
1066
1067  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1068    if (unformat (line_input, "del"))
1069      is_add = 0;
1070    else if (unformat (line_input, "version %d", &tmp))
1071      ver_o_c |= (tmp & 3) << 6;
1072    else if (unformat (line_input, "o-bit %d", &tmp))
1073      ver_o_c |= (tmp & 1) << 5;
1074    else if (unformat (line_input, "c-bit %d", &tmp))
1075      ver_o_c |= (tmp & 1) << 4;
1076    else if (unformat (line_input, "md-type %d", &tmp))
1077      md_type = tmp;
1078    else if (unformat(line_input, "next-ip4"))
1079      next_protocol = 1;
1080    else if (unformat(line_input, "next-ip6"))
1081      next_protocol = 2;
1082    else if (unformat(line_input, "next-ethernet"))
1083      next_protocol = 3;
1084    else if (unformat (line_input, "c1 %d", &c1))
1085      ;
1086    else if (unformat (line_input, "c2 %d", &c2))
1087      ;
1088    else if (unformat (line_input, "c3 %d", &c3))
1089      ;
1090    else if (unformat (line_input, "c4 %d", &c4))
1091      ;
1092    else if (unformat (line_input, "nsp %d", &nsp))
1093      nsp_set = 1;
1094    else if (unformat (line_input, "nsi %d", &nsi))
1095      nsi_set = 1;
1096    else if (unformat (line_input, "tlv-ioam-trace"))
1097      has_ioam_trace_option = 1;
1098    else
1099      return clib_error_return (0, "parse error: '%U'",
1100                                format_unformat_error, line_input);
1101  }
1102
1103  unformat_free (line_input);
1104
1105  if (nsp_set == 0)
1106    return clib_error_return (0, "nsp not specified");
1107
1108  if (nsi_set == 0)
1109    return clib_error_return (0, "nsi not specified");
1110
1111  if (md_type == 1 && has_ioam_trace_option == 1)
1112    return clib_error_return (0, "Invalid MD Type");
1113
1114  nsp_nsi = (nsp<<8) | nsi;
1115
1116  memset (a, 0, sizeof (*a));
1117  a->is_add = is_add;
1118
1119  if (md_type == 1)
1120    {
1121      a->nsh_entry.md.md1_data.c1 = c1;
1122      a->nsh_entry.md.md1_data.c2 = c2;
1123      a->nsh_entry.md.md1_data.c3 = c3;
1124      a->nsh_entry.md.md1_data.c4 = c4;
1125      length = (sizeof(nsh_base_header_t)+sizeof(nsh_md1_data_t)) >> 2;
1126    }
1127  else if (md_type == 2)
1128    {
1129      length = sizeof(nsh_base_header_t) >> 2;
1130
1131      vec_free(a->nsh_entry.tlvs_data);
1132      tlvs_len = (MAX_METADATA_LEN << 2);
1133      vec_validate_aligned (data, tlvs_len-1, CLIB_CACHE_LINE_BYTES);
1134      a->nsh_entry.tlvs_data = data;
1135      current = data;
1136
1137      if(has_ioam_trace_option)
1138	{
1139	  tlv_header.class = clib_host_to_net_u16(NSH_MD2_IOAM_CLASS);
1140          tlv_header.type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
1141          /* Uses network order's class and type to lookup */
1142          nsh_option = nsh_md2_lookup_option(tlv_header.class, tlv_header.type);
1143          if( nsh_option == NULL)
1144              return clib_error_return (0, "iOAM Trace not registered");
1145
1146          if(nm->add_options[nsh_option->option_id] != NULL)
1147            {
1148              if (0 != nm->add_options[nsh_option->option_id] (
1149        	  (u8 *)current, &option_size ))
1150                {
1151        	  return clib_error_return (0, "Invalid MD Type");
1152                }
1153            }
1154
1155          nm->options_size[nsh_option->option_id]=  option_size;
1156          /* round to 4-byte */
1157          option_size = ( ((option_size+3)>>2) << 2 );
1158
1159          cur_len += option_size;
1160          current = data + option_size;
1161	}
1162
1163      /* Add more options' parsing */
1164
1165      a->nsh_entry.tlvs_len = cur_len;
1166      length += (cur_len >> 2);
1167    }
1168
1169#define _(x) a->nsh_entry.nsh_base.x = x;
1170  foreach_copy_nsh_base_hdr_field;
1171#undef _
1172
1173  rv = nsh_add_del_entry (a, &entry_index);
1174
1175  switch(rv)
1176    {
1177    case 0:
1178      break;
1179    default:
1180      return clib_error_return
1181        (0, "nsh_add_del_entry returned %d", rv);
1182    }
1183
1184  return 0;
1185}
1186
1187VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
1188  .path = "create nsh entry",
1189  .short_help =
1190  "create nsh entry {nsp <nn> nsi <nn>} [md-type <nn>]"
1191  "  [c1 <nn> c2 <nn> c3 <nn> c4 <nn>] [tlv-ioam-trace] [del]\n",
1192  .function = nsh_add_del_entry_command_fn,
1193};
1194
1195/** API message handler */
1196static void vl_api_nsh_add_del_entry_t_handler
1197(vl_api_nsh_add_del_entry_t * mp)
1198{
1199  vl_api_nsh_add_del_entry_reply_t * rmp;
1200  nsh_main_t * nm = &nsh_main;
1201  int rv;
1202  nsh_add_del_entry_args_t _a, *a = &_a;
1203  u32 entry_index = ~0;
1204  u8 tlvs_len = 0;
1205  u8 * data = 0;
1206
1207  a->is_add = mp->is_add;
1208  a->nsh_entry.nsh_base.ver_o_c = mp->ver_o_c;
1209  a->nsh_entry.nsh_base.length = mp->length;
1210  a->nsh_entry.nsh_base.md_type = mp->md_type;
1211  a->nsh_entry.nsh_base.next_protocol = mp->next_protocol;
1212  a->nsh_entry.nsh_base.nsp_nsi = ntohl(mp->nsp_nsi);
1213  if (mp->md_type == 1)
1214    {
1215      a->nsh_entry.md.md1_data.c1 = ntohl(mp->c1);
1216      a->nsh_entry.md.md1_data.c2 = ntohl(mp->c2);
1217      a->nsh_entry.md.md1_data.c3 = ntohl(mp->c3);
1218      a->nsh_entry.md.md1_data.c4 = ntohl(mp->c4);
1219    }
1220  else if (mp->md_type == 2)
1221    {
1222      tlvs_len = mp->tlv_length;
1223      vec_validate_aligned (data, tlvs_len-1, CLIB_CACHE_LINE_BYTES);
1224
1225      clib_memcpy(data, mp->tlv, tlvs_len);
1226      a->nsh_entry.tlvs_data = data;
1227      a->nsh_entry.tlvs_len = tlvs_len;
1228    }
1229
1230  rv = nsh_add_del_entry (a, &entry_index);
1231
1232  REPLY_MACRO2(VL_API_NSH_ADD_DEL_ENTRY_REPLY,
1233  ({
1234    rmp->entry_index = htonl (entry_index);
1235  }));
1236}
1237
1238static void send_nsh_entry_details
1239(nsh_entry_t * t, unix_shared_memory_queue_t * q, u32 context)
1240{
1241    vl_api_nsh_entry_details_t * rmp;
1242    nsh_main_t * nm = &nsh_main;
1243
1244    rmp = vl_msg_api_alloc (sizeof (*rmp));
1245    memset (rmp, 0, sizeof (*rmp));
1246
1247    rmp->_vl_msg_id = ntohs((VL_API_NSH_ENTRY_DETAILS)+nm->msg_id_base);
1248    rmp->ver_o_c = t->nsh_base.ver_o_c;
1249    rmp->length = t->nsh_base.length;
1250    rmp->md_type = t->nsh_base.md_type;
1251    rmp->next_protocol = t->nsh_base.next_protocol;
1252    rmp->nsp_nsi = htonl(t->nsh_base.nsp_nsi);
1253
1254    if (t->nsh_base.md_type == 1)
1255      {
1256	rmp->tlv_length = 4;
1257        rmp->c1 = htonl(t->md.md1_data.c1);
1258        rmp->c2 = htonl(t->md.md1_data.c2);
1259        rmp->c3 = htonl(t->md.md1_data.c3);
1260        rmp->c4 = htonl(t->md.md1_data.c4);
1261      }
1262    else if (t->nsh_base.md_type == 2)
1263      {
1264	rmp->tlv_length = t->tlvs_len;
1265	clib_memcpy(rmp->tlv, t->tlvs_data, t->tlvs_len);
1266      }
1267
1268    rmp->context = context;
1269
1270    vl_msg_api_send_shmem (q, (u8 *)&rmp);
1271}
1272
1273static void vl_api_nsh_entry_dump_t_handler
1274(vl_api_nsh_entry_dump_t * mp)
1275{
1276    unix_shared_memory_queue_t * q;
1277    nsh_main_t * nm = &nsh_main;
1278    nsh_entry_t * t;
1279    u32 entry_index;
1280
1281    q = vl_api_client_index_to_input_queue (mp->client_index);
1282    if (q == 0) {
1283        return;
1284    }
1285
1286    entry_index = ntohl (mp->entry_index);
1287
1288    if (~0 == entry_index)
1289      {
1290	pool_foreach (t, nm->nsh_entries,
1291	({
1292	    send_nsh_entry_details(t, q, mp->context);
1293	}));
1294      }
1295    else
1296      {
1297        if (entry_index >= vec_len (nm->nsh_entries))
1298  	{
1299  	  return;
1300  	}
1301        t = &nm->nsh_entries[entry_index];
1302        send_nsh_entry_details(t, q, mp->context);
1303      }
1304}
1305
1306static void send_nsh_map_details
1307(nsh_map_t * t, unix_shared_memory_queue_t * q, u32 context)
1308{
1309    vl_api_nsh_map_details_t * rmp;
1310    nsh_main_t * nm = &nsh_main;
1311
1312    rmp = vl_msg_api_alloc (sizeof (*rmp));
1313    memset (rmp, 0, sizeof (*rmp));
1314
1315    rmp->_vl_msg_id = ntohs((VL_API_NSH_MAP_DETAILS)+nm->msg_id_base);
1316    rmp->nsp_nsi = htonl(t->nsp_nsi);
1317    rmp->mapped_nsp_nsi = htonl(t->mapped_nsp_nsi);
1318    rmp->nsh_action = htonl(t->nsh_action);
1319    rmp->sw_if_index = htonl(t->sw_if_index);
1320    rmp->rx_sw_if_index = htonl(t->rx_sw_if_index);
1321    rmp->next_node = htonl(t->next_node);
1322
1323    rmp->context = context;
1324
1325    vl_msg_api_send_shmem (q, (u8 *)&rmp);
1326}
1327
1328static void vl_api_nsh_map_dump_t_handler
1329(vl_api_nsh_map_dump_t * mp)
1330{
1331    unix_shared_memory_queue_t * q;
1332    nsh_main_t * nm = &nsh_main;
1333    nsh_map_t * t;
1334    u32 map_index;
1335
1336    q = vl_api_client_index_to_input_queue (mp->client_index);
1337    if (q == 0) {
1338        return;
1339    }
1340
1341    map_index = ntohl (mp->map_index);
1342
1343    if (~0 == map_index)
1344      {
1345	pool_foreach (t, nm->nsh_mappings,
1346	({
1347	    send_nsh_map_details(t, q, mp->context);
1348	}));
1349      }
1350    else
1351      {
1352        if (map_index >= vec_len (nm->nsh_mappings))
1353  	{
1354  	  return;
1355  	}
1356        t = &nm->nsh_mappings[map_index];
1357        send_nsh_map_details(t, q, mp->context);
1358      }
1359}
1360
1361static clib_error_t *
1362show_nsh_entry_command_fn (vlib_main_t * vm,
1363			   unformat_input_t * input,
1364			   vlib_cli_command_t * cmd)
1365{
1366  nsh_main_t * nm = &nsh_main;
1367  nsh_entry_t * nsh_entry;
1368
1369  if (pool_elts (nm->nsh_entries) == 0)
1370    vlib_cli_output (vm, "No nsh entries configured.");
1371
1372  pool_foreach (nsh_entry, nm->nsh_entries,
1373		({
1374		  vlib_cli_output (vm, "%U", format_nsh_header,
1375				   nsh_entry->rewrite );
1376
1377		  vlib_cli_output (vm, "  rewrite_size: %d bytes",
1378				   nsh_entry->rewrite_size);
1379		}));
1380
1381  return 0;
1382}
1383
1384VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
1385  .path = "show nsh entry",
1386  .function = show_nsh_entry_command_fn,
1387};
1388
1389
1390/* Set up the API message handling tables */
1391static clib_error_t *
1392nsh_plugin_api_hookup (vlib_main_t *vm)
1393{
1394  nsh_main_t * nm __attribute__ ((unused)) = &nsh_main;
1395#define _(N,n)                                                  \
1396  vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base),	\
1397			  #n,					\
1398			  vl_api_##n##_t_handler,		\
1399			  vl_noop_handler,			\
1400			  vl_api_##n##_t_endian,		\
1401			  vl_api_##n##_t_print,			\
1402			  sizeof(vl_api_##n##_t), 1);
1403  foreach_nsh_plugin_api_msg;
1404#undef _
1405
1406  return 0;
1407}
1408
1409static void
1410setup_message_id_table (nsh_main_t * nm, api_main_t * am)
1411{
1412#define _(id,n,crc) \
1413  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nm->msg_id_base);
1414  foreach_vl_msg_name_crc_nsh;
1415#undef _
1416}
1417
1418always_inline void
1419nsh_md2_encap (vlib_buffer_t * b, nsh_base_header_t *hdr,
1420	       nsh_entry_t * nsh_entry)
1421{
1422  nsh_main_t *nm = &nsh_main;
1423  nsh_base_header_t * nsh_base;
1424  nsh_tlv_header_t * opt0;
1425  nsh_tlv_header_t * limit0;
1426  nsh_tlv_header_t * nsh_md2;
1427  nsh_option_map_t * nsh_option;
1428  u8 old_option_size = 0;
1429  u8 new_option_size = 0;
1430
1431  /* Populate the NSH Header */
1432  opt0 = (nsh_tlv_header_t *)(nsh_entry->tlvs_data);
1433  limit0 = (nsh_tlv_header_t *) (nsh_entry->tlvs_data + nsh_entry->tlvs_len);
1434
1435  nsh_md2 = (nsh_tlv_header_t *)((u8 *)hdr /*nsh_entry->rewrite*/ + sizeof(nsh_base_header_t));
1436  nsh_entry->rewrite_size = sizeof(nsh_base_header_t);
1437
1438  /* Scan the set of variable metadata, process ones that we understand */
1439  while (opt0 < limit0)
1440    {
1441      old_option_size = sizeof (nsh_tlv_header_t) + opt0->length;
1442      /* round to 4-byte */
1443      old_option_size = ( (old_option_size+3)>>2 ) << 2;
1444
1445      nsh_option = nsh_md2_lookup_option(opt0->class, opt0->type);
1446      if( nsh_option == NULL)
1447	{
1448	  goto next_tlv_md2;
1449	}
1450
1451      if (nm->options[nsh_option->option_id])
1452	{
1453	  if ( (*nm->options[nsh_option->option_id]) (b, nsh_md2) )
1454	    {
1455	      goto next_tlv_md2;
1456	    }
1457
1458	  /* option length may be varied */
1459	  new_option_size = sizeof (nsh_tlv_header_t) + nsh_md2->length;
1460	  /* round to 4-byte */
1461	  new_option_size = ( (new_option_size+3)>>2 ) << 2;
1462	  nsh_entry->rewrite_size += new_option_size;
1463
1464	  nsh_md2 = (nsh_tlv_header_t *) (((u8 *) nsh_md2) + new_option_size);
1465	  opt0 = (nsh_tlv_header_t *) (((u8 *) opt0) + old_option_size);
1466
1467	}
1468      else
1469	{
1470next_tlv_md2:
1471	  opt0 = (nsh_tlv_header_t *) (((u8 *) opt0) + old_option_size);
1472	}
1473    }
1474
1475  /* update nsh header's length */
1476  nsh_base = (nsh_base_header_t *)nsh_entry->rewrite;
1477  nsh_base->length = (nsh_entry->rewrite_size >> 2);
1478
1479  return;
1480}
1481
1482always_inline void
1483nsh_md2_swap (vlib_buffer_t * b,
1484              nsh_base_header_t * hdr,
1485	      u32 header_len,
1486	      nsh_entry_t * nsh_entry,
1487	      u32 * next,
1488	      u32 drop_node_val)
1489{
1490  nsh_main_t *nm = &nsh_main;
1491  nsh_base_header_t * nsh_base;
1492  nsh_tlv_header_t * opt0;
1493  nsh_tlv_header_t * limit0;
1494  nsh_tlv_header_t * nsh_md2;
1495  nsh_option_map_t * nsh_option;
1496  u8 old_option_size = 0;
1497  u8 new_option_size = 0;
1498
1499  /* Populate the NSH Header */
1500  opt0 = (nsh_md2_data_t *)(hdr + 1);
1501  limit0 = (nsh_md2_data_t *) ((u8 *) hdr + header_len);
1502
1503  nsh_md2 = (nsh_tlv_header_t *)(nsh_entry->rewrite + sizeof(nsh_base_header_t));
1504  nsh_entry->rewrite_size = sizeof(nsh_base_header_t);
1505
1506  /* Scan the set of variable metadata, process ones that we understand */
1507  while (opt0 < limit0)
1508    {
1509      old_option_size = sizeof (nsh_tlv_header_t) + opt0->length;
1510      /* round to 4-byte */
1511      old_option_size = ( (old_option_size+3)>>2 ) << 2;
1512
1513      nsh_option = nsh_md2_lookup_option(opt0->class, opt0->type);
1514      if( nsh_option == NULL)
1515	{
1516	  goto next_tlv_md2;
1517	}
1518
1519      if (nm->swap_options[nsh_option->option_id])
1520	{
1521	  if ( (*nm->swap_options[nsh_option->option_id]) (b, opt0, nsh_md2) )
1522	    {
1523	      goto next_tlv_md2;
1524	    }
1525
1526	  /* option length may be varied */
1527	  new_option_size = sizeof (nsh_tlv_header_t) + nsh_md2->length;
1528	  /* round to 4-byte */
1529	  new_option_size = ( (new_option_size+3)>>2 ) << 2;
1530	  nsh_entry->rewrite_size += new_option_size;
1531	  nsh_md2 = (nsh_tlv_header_t *) (((u8 *) nsh_md2) + new_option_size);
1532
1533	  opt0 = (nsh_tlv_header_t *) (((u8 *) opt0) + old_option_size);
1534
1535	}
1536      else
1537	{
1538next_tlv_md2:
1539	  opt0 = (nsh_tlv_header_t *) (((u8 *) opt0) + old_option_size);
1540	}
1541    }
1542
1543  /* update nsh header's length */
1544  nsh_base = (nsh_base_header_t *)nsh_entry->rewrite;
1545  nsh_base->length = (nsh_entry->rewrite_size >> 2);
1546  return;
1547}
1548
1549always_inline void
1550nsh_md2_decap (vlib_buffer_t * b,
1551               nsh_base_header_t * hdr,
1552	       u32 *header_len,
1553	       u32 * next,
1554	       u32 drop_node_val)
1555{
1556  nsh_main_t *nm = &nsh_main;
1557  nsh_md2_data_t *opt0;
1558  nsh_md2_data_t *limit0;
1559  nsh_option_map_t *nsh_option;
1560  u8 option_len = 0;
1561
1562  /* Populate the NSH Header */
1563  opt0 = (nsh_md2_data_t *)(hdr + 1);
1564  limit0 = (nsh_md2_data_t *) ((u8 *) hdr + *header_len);
1565
1566  /* Scan the set of variable metadata, process ones that we understand */
1567  while (opt0 < limit0)
1568    {
1569      nsh_option = nsh_md2_lookup_option(opt0->class, opt0->type);
1570      if( nsh_option == NULL)
1571	{
1572	  *next = drop_node_val;
1573	  return;
1574	}
1575
1576	  if (nm->pop_options[nsh_option->option_id])
1577	    {
1578	      if ( (*nm->pop_options[nsh_option->option_id]) (b, opt0) )
1579		{
1580		  *next = drop_node_val;
1581		  return;
1582		}
1583	    }
1584      /* round to 4-byte */
1585      option_len = ( (opt0->length+3)>>2 ) << 2;
1586      opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + sizeof (nsh_md2_data_t) + option_len);
1587      *next = (nm->decap_v4_next_override) ? (nm->decap_v4_next_override) : (*next);
1588      *header_len = (nm->decap_v4_next_override) ? 0 : (*header_len);
1589    }
1590
1591  return;
1592}
1593
1594static uword
1595nsh_input_map (vlib_main_t * vm,
1596               vlib_node_runtime_t * node,
1597               vlib_frame_t * from_frame,
1598	       u32 node_type)
1599{
1600  u32 n_left_from, next_index, *from, *to_next;
1601  nsh_main_t * nm = &nsh_main;
1602
1603  from = vlib_frame_vector_args(from_frame);
1604  n_left_from = from_frame->n_vectors;
1605
1606  next_index = node->cached_next_index;
1607
1608  while (n_left_from > 0)
1609    {
1610      u32 n_left_to_next;
1611
1612      vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
1613
1614      while (n_left_from >= 4 && n_left_to_next >= 2)
1615	{
1616	  u32 bi0, bi1;
1617	  vlib_buffer_t * b0, *b1;
1618	  u32 next0 = NSH_NODE_NEXT_DROP, next1 = NSH_NODE_NEXT_DROP;
1619	  uword * entry0, *entry1;
1620	  nsh_base_header_t * hdr0 = 0, *hdr1 = 0;
1621	  u32 header_len0 = 0, header_len1 = 0;
1622	  u32 nsp_nsi0, nsp_nsi1;
1623	  u32 error0, error1;
1624	  nsh_map_t * map0 = 0, *map1 = 0;
1625	  nsh_entry_t * nsh_entry0 = 0, *nsh_entry1 = 0;
1626	  nsh_base_header_t *encap_hdr0 = 0, *encap_hdr1 = 0;
1627	  u32 encap_hdr_len0 = 0, encap_hdr_len1 = 0;
1628	  nsh_proxy_session_by_key_t key0, key1;
1629	  uword *p0, *p1;
1630	  nsh_proxy_session_t *proxy0, *proxy1;
1631	  u32 sw_if_index0 = 0, sw_if_index1 = 0;
1632	  ethernet_header_t dummy_eth0, dummy_eth1;
1633
1634	  /* Prefetch next iteration. */
1635	  {
1636	    vlib_buffer_t * p2, *p3;
1637
1638	    p2 = vlib_get_buffer(vm, from[2]);
1639	    p3 = vlib_get_buffer(vm, from[3]);
1640
1641	    vlib_prefetch_buffer_header(p2, LOAD);
1642	    vlib_prefetch_buffer_header(p3, LOAD);
1643
1644	    CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1645	    CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
1646	  }
1647
1648	  bi0 = from[0];
1649	  bi1 = from[1];
1650	  to_next[0] = bi0;
1651	  to_next[1] = bi1;
1652	  from += 2;
1653	  to_next += 2;
1654	  n_left_from -= 2;
1655	  n_left_to_next -= 2;
1656
1657	  error0 = 0;
1658	  error1 = 0;
1659
1660	  b0 = vlib_get_buffer(vm, bi0);
1661	  b1 = vlib_get_buffer(vm, bi1);
1662	  hdr0 = vlib_buffer_get_current(b0);
1663          if(node_type == NSH_INPUT_TYPE)
1664            {
1665              nsp_nsi0 = hdr0->nsp_nsi;
1666              header_len0 = hdr0->length * 4;
1667            }
1668          else if(node_type == NSH_CLASSIFIER_TYPE)
1669            {
1670              nsp_nsi0 = clib_host_to_net_u32(vnet_buffer(b0)->l2_classify.opaque_index);
1671            }
1672          else if(node_type == NSH_AWARE_VNF_PROXY_TYPE)
1673            {
1674	      /* Push dummy Eth header */
1675              memset(&dummy_eth0.dst_address[0], 0x11223344, 4);
1676              memset(&dummy_eth0.dst_address[4], 0x5566, 2);
1677              memset(&dummy_eth0.src_address[0], 0x778899aa, 4);
1678              memset(&dummy_eth0.src_address[4], 0xbbcc, 2);
1679              dummy_eth0.type = 0x0800;
1680	      vlib_buffer_advance(b0, -(word)sizeof(ethernet_header_t));
1681	      hdr0 = vlib_buffer_get_current(b0);
1682	      clib_memcpy(hdr0, &dummy_eth0, (word)sizeof(ethernet_header_t));
1683
1684              sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
1685              nsp_nsi0 = nm->tunnel_index_by_sw_if_index[sw_if_index0];
1686            }
1687          else
1688	    {
1689	      memset (&key0, 0, sizeof(key0));
1690	      key0.transport_type = NSH_NODE_NEXT_ENCAP_VXLAN4;
1691              key0.transport_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1692
1693              p0 = hash_get_mem(nm->nsh_proxy_session_by_key, &key0);
1694	      if (PREDICT_FALSE(p0 == 0))
1695		{
1696		  error0 = NSH_NODE_ERROR_NO_PROXY;
1697		  goto trace0;
1698		}
1699
1700	      proxy0 = pool_elt_at_index(nm->nsh_proxy_sessions, p0[0]);
1701              if (PREDICT_FALSE(proxy0 == 0))
1702                {
1703                  error0 = NSH_NODE_ERROR_NO_PROXY;
1704                  goto trace0;
1705                }
1706              nsp_nsi0 = proxy0->nsp_nsi;
1707	    }
1708
1709          hdr1 = vlib_buffer_get_current(b1);
1710          if(node_type == NSH_INPUT_TYPE)
1711	    {
1712	      nsp_nsi1 = hdr1->nsp_nsi;
1713	      header_len1 = hdr1->length * 4;
1714	    }
1715          else if(node_type == NSH_CLASSIFIER_TYPE)
1716            {
1717              nsp_nsi1 = clib_host_to_net_u32(vnet_buffer(b1)->l2_classify.opaque_index);
1718            }
1719          else if(node_type == NSH_AWARE_VNF_PROXY_TYPE)
1720            {
1721	      /* Push dummy Eth header */
1722              memset(&dummy_eth1.dst_address[0], 0x11223344, 4);
1723              memset(&dummy_eth1.dst_address[4], 0x5566, 2);
1724              memset(&dummy_eth1.src_address[0], 0x778899aa, 4);
1725              memset(&dummy_eth1.src_address[4], 0xbbcc, 2);
1726              dummy_eth1.type = 0x0800;
1727	      vlib_buffer_advance(b1, -(word)sizeof(ethernet_header_t));
1728	      hdr1 = vlib_buffer_get_current(b1);
1729	      clib_memcpy(hdr1, &dummy_eth1, (word)sizeof(ethernet_header_t));
1730
1731              sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
1732              nsp_nsi1 = nm->tunnel_index_by_sw_if_index[sw_if_index1];
1733            }
1734          else
1735	    {
1736	      memset (&key1, 0, sizeof(key1));
1737	      key1.transport_type = NSH_NODE_NEXT_ENCAP_VXLAN4;
1738              key1.transport_index = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1739
1740              p1 = hash_get_mem(nm->nsh_proxy_session_by_key, &key1);
1741	      if (PREDICT_FALSE(p1 == 0))
1742		{
1743		  error1 = NSH_NODE_ERROR_NO_PROXY;
1744		  goto trace1;
1745		}
1746
1747	      proxy1 = pool_elt_at_index(nm->nsh_proxy_sessions, p1[0]);
1748              if (PREDICT_FALSE(proxy1 == 0))
1749                {
1750                  error1 = NSH_NODE_ERROR_NO_PROXY;
1751                  goto trace1;
1752                }
1753              nsp_nsi1 = proxy1->nsp_nsi;
1754	    }
1755
1756	  /* Process packet 0 */
1757	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
1758	  if (PREDICT_FALSE(entry0 == 0))
1759	    {
1760	      error0 = NSH_NODE_ERROR_NO_MAPPING;
1761	      goto trace0;
1762	    }
1763
1764	  /* Entry should point to a mapping ...*/
1765	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
1766	  if (PREDICT_FALSE(map0 == 0))
1767	    {
1768	      error0 = NSH_NODE_ERROR_NO_MAPPING;
1769	      goto trace0;
1770	    }
1771
1772	  /* set up things for next node to transmit ie which node to handle it and where */
1773	  next0 = map0->next_node;
1774	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
1775
1776	  if(PREDICT_FALSE(map0->nsh_action == NSH_ACTION_POP))
1777	    {
1778	      /* Manipulate MD2 */
1779              if(PREDICT_FALSE(hdr0->md_type == 2))
1780        	{
1781        	  nsh_md2_decap(b0, hdr0, &header_len0, &next0, NSH_NODE_NEXT_DROP);
1782        	  if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
1783        	    {
1784        	      error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
1785        	      goto trace0;
1786        	    }
1787	          vnet_buffer(b0)->sw_if_index[VLIB_RX] = map0->rx_sw_if_index;
1788        	}
1789
1790              /* Pop NSH header */
1791	      vlib_buffer_advance(b0, (word)header_len0);
1792	      goto trace0;
1793	    }
1794
1795	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
1796	  if (PREDICT_FALSE(entry0 == 0))
1797	    {
1798	      error0 = NSH_NODE_ERROR_NO_ENTRY;
1799	      goto trace0;
1800	    }
1801
1802	  nsh_entry0 = (nsh_entry_t *)pool_elt_at_index(nm->nsh_entries, entry0[0]);
1803	  encap_hdr0 = (nsh_base_header_t *)(nsh_entry0->rewrite);
1804	  /* rewrite_size should equal to (encap_hdr0->length * 4) */
1805	  encap_hdr_len0 = nsh_entry0->rewrite_size;
1806
1807	  if(PREDICT_TRUE(map0->nsh_action == NSH_ACTION_SWAP))
1808	    {
1809	      /* Manipulate MD2 */
1810              if(PREDICT_FALSE(hdr0->md_type == 2))
1811        	{
1812        	  nsh_md2_swap(b0, hdr0, header_len0, nsh_entry0,
1813        	               &next0, NSH_NODE_NEXT_DROP);
1814        	  if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
1815        	    {
1816        	      error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
1817        	      goto trace0;
1818        	    }
1819        	}
1820
1821              /* Pop old NSH header */
1822	      vlib_buffer_advance(b0, (word)header_len0);
1823
1824              /* After processing, md2's length may be varied */
1825              encap_hdr_len0 = nsh_entry0->rewrite_size;
1826	      /* Push new NSH header */
1827	      vlib_buffer_advance(b0, -(word)encap_hdr_len0);
1828	      hdr0 = vlib_buffer_get_current(b0);
1829	      clib_memcpy(hdr0, encap_hdr0, (word)encap_hdr_len0);
1830
1831	      goto trace0;
1832	    }
1833
1834	  if(PREDICT_TRUE(map0->nsh_action == NSH_ACTION_PUSH))
1835	    {
1836              /* After processing, md2's length may be varied */
1837              encap_hdr_len0 = nsh_entry0->rewrite_size;
1838	      /* Push new NSH header */
1839	      vlib_buffer_advance(b0, -(word)encap_hdr_len0);
1840	      hdr0 = vlib_buffer_get_current(b0);
1841	      clib_memcpy(hdr0, encap_hdr0, (word)encap_hdr_len0);
1842
1843	      /* Manipulate MD2 */
1844              if(PREDICT_FALSE(nsh_entry0->nsh_base.md_type == 2))
1845        	{
1846        	  nsh_md2_encap(b0, hdr0, nsh_entry0);
1847        	}
1848
1849	    }
1850
1851        trace0: b0->error = error0 ? node->errors[error0] : 0;
1852
1853          if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1854            {
1855              nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
1856              clib_memcpy ( &(tr->trace_data), hdr0, (hdr0->length*4) );
1857            }
1858
1859	  /* Process packet 1 */
1860	  entry1 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi1);
1861	  if (PREDICT_FALSE(entry1 == 0))
1862	    {
1863	      error1 = NSH_NODE_ERROR_NO_MAPPING;
1864	      goto trace1;
1865	    }
1866
1867	  /* Entry should point to a mapping ...*/
1868	  map1 = pool_elt_at_index(nm->nsh_mappings, entry1[0]);
1869	  if (PREDICT_FALSE(map1 == 0))
1870	    {
1871	      error1 = NSH_NODE_ERROR_NO_MAPPING;
1872	      goto trace1;
1873	    }
1874
1875	  /* set up things for next node to transmit ie which node to handle it and where */
1876	  next1 = map1->next_node;
1877	  vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
1878
1879	  if(PREDICT_FALSE(map1->nsh_action == NSH_ACTION_POP))
1880	    {
1881	      /* Manipulate MD2 */
1882              if(PREDICT_FALSE(hdr1->md_type == 2))
1883        	{
1884        	  nsh_md2_decap(b1, hdr1, &header_len1, &next1, NSH_NODE_NEXT_DROP);
1885        	  if (PREDICT_FALSE(next1 == NSH_NODE_NEXT_DROP))
1886        	    {
1887        	      error1 = NSH_NODE_ERROR_INVALID_OPTIONS;
1888        	      goto trace1;
1889        	    }
1890	          vnet_buffer(b1)->sw_if_index[VLIB_RX] = map0->rx_sw_if_index;
1891        	}
1892
1893              /* Pop NSH header */
1894	      vlib_buffer_advance(b1, (word)header_len1);
1895	      goto trace1;
1896	    }
1897
1898	  entry1 = hash_get_mem(nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
1899	  if (PREDICT_FALSE(entry1 == 0))
1900	    {
1901	      error1 = NSH_NODE_ERROR_NO_ENTRY;
1902	      goto trace1;
1903	    }
1904
1905	  nsh_entry1 = (nsh_entry_t *)pool_elt_at_index(nm->nsh_entries, entry1[0]);
1906	  encap_hdr1 = (nsh_base_header_t *)(nsh_entry1->rewrite);
1907	  /* rewrite_size should equal to (encap_hdr0->length * 4) */
1908	  encap_hdr_len1 = nsh_entry1->rewrite_size;
1909
1910          if(PREDICT_TRUE(map1->nsh_action == NSH_ACTION_SWAP))
1911            {
1912	      /* Manipulate MD2 */
1913              if(PREDICT_FALSE(hdr1->md_type == 2))
1914        	{
1915        	  nsh_md2_swap(b1, hdr1, header_len1, nsh_entry1,
1916        	               &next1, NSH_NODE_NEXT_DROP);
1917        	  if (PREDICT_FALSE(next1 == NSH_NODE_NEXT_DROP))
1918        	    {
1919        	      error1 = NSH_NODE_ERROR_INVALID_OPTIONS;
1920        	      goto trace1;
1921        	    }
1922        	}
1923
1924              /* Pop old NSH header */
1925	      vlib_buffer_advance(b1, (word)header_len1);
1926
1927              /* After processing, md2's length may be varied */
1928              encap_hdr_len1 = nsh_entry1->rewrite_size;
1929	      /* Push new NSH header */
1930	      vlib_buffer_advance(b1, -(word)encap_hdr_len1);
1931	      hdr1 = vlib_buffer_get_current(b1);
1932	      clib_memcpy(hdr1, encap_hdr1, (word)encap_hdr_len1);
1933
1934	      goto trace1;
1935	    }
1936
1937          if(PREDICT_FALSE(map1->nsh_action == NSH_ACTION_PUSH))
1938            {
1939              /* After processing, md2's length may be varied */
1940              encap_hdr_len1 = nsh_entry1->rewrite_size;
1941	      /* Push new NSH header */
1942	      vlib_buffer_advance(b1, -(word)encap_hdr_len1);
1943	      hdr1 = vlib_buffer_get_current(b1);
1944	      clib_memcpy(hdr1, encap_hdr1, (word)encap_hdr_len1);
1945
1946	      /* Manipulate MD2 */
1947              if(PREDICT_FALSE(nsh_entry1->nsh_base.md_type == 2))
1948        	{
1949        	  nsh_md2_encap(b1, hdr1, nsh_entry1);
1950        	}
1951
1952	    }
1953
1954	trace1: b1->error = error1 ? node->errors[error1] : 0;
1955
1956	  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
1957	    {
1958	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
1959	      clib_memcpy ( &(tr->trace_data), hdr1, (hdr1->length*4) );
1960	    }
1961
1962	  vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
1963					  n_left_to_next, bi0, bi1, next0, next1);
1964
1965	}
1966
1967      while (n_left_from > 0 && n_left_to_next > 0)
1968	{
1969	  u32 bi0 = 0;
1970	  vlib_buffer_t * b0 = NULL;
1971	  u32 next0 = NSH_NODE_NEXT_DROP;
1972	  uword * entry0;
1973	  nsh_base_header_t * hdr0 = 0;
1974	  u32 header_len0 = 0;
1975	  u32 nsp_nsi0;
1976	  u32 error0;
1977	  nsh_map_t * map0 = 0;
1978	  nsh_entry_t * nsh_entry0 = 0;
1979	  nsh_base_header_t * encap_hdr0 = 0;
1980	  u32 encap_hdr_len0 = 0;
1981	  nsh_proxy_session_by_key_t key0;
1982	  uword *p0;
1983	  nsh_proxy_session_t *proxy0 = 0;
1984	  u32 sw_if_index0 = 0;
1985	  ethernet_header_t dummy_eth0;
1986
1987	  bi0 = from[0];
1988	  to_next[0] = bi0;
1989	  from += 1;
1990	  to_next += 1;
1991	  n_left_from -= 1;
1992	  n_left_to_next -= 1;
1993	  error0 = 0;
1994
1995	  b0 = vlib_get_buffer(vm, bi0);
1996	  hdr0 = vlib_buffer_get_current(b0);
1997
1998          if(node_type == NSH_INPUT_TYPE)
1999            {
2000              nsp_nsi0 = hdr0->nsp_nsi;
2001              header_len0 = hdr0->length * 4;
2002            }
2003          else if(node_type == NSH_CLASSIFIER_TYPE)
2004            {
2005              nsp_nsi0 = clib_host_to_net_u32(vnet_buffer(b0)->l2_classify.opaque_index);
2006            }
2007          else if(node_type == NSH_AWARE_VNF_PROXY_TYPE)
2008            {
2009	      /* Push dummy Eth header */
2010              memset(&dummy_eth0.dst_address[0], 0x11223344, 4);
2011              memset(&dummy_eth0.dst_address[4], 0x5566, 2);
2012              memset(&dummy_eth0.src_address[0], 0x778899aa, 4);
2013              memset(&dummy_eth0.src_address[4], 0xbbcc, 2);
2014              dummy_eth0.type = 0x0800;
2015	      vlib_buffer_advance(b0, -(word)sizeof(ethernet_header_t));
2016	      hdr0 = vlib_buffer_get_current(b0);
2017	      clib_memcpy(hdr0, &dummy_eth0, (word)sizeof(ethernet_header_t));
2018
2019              sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
2020              nsp_nsi0 = nm->tunnel_index_by_sw_if_index[sw_if_index0];
2021            }
2022          else
2023	    {
2024	      memset (&key0, 0, sizeof(key0));
2025	      key0.transport_type = NSH_NODE_NEXT_ENCAP_VXLAN4;
2026	      key0.transport_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2027
2028	      p0 = hash_get_mem(nm->nsh_proxy_session_by_key, &key0);
2029	      if (PREDICT_FALSE(p0 == 0))
2030		{
2031		  error0 = NSH_NODE_ERROR_NO_PROXY;
2032		  goto trace00;
2033		}
2034
2035	      proxy0 = pool_elt_at_index(nm->nsh_proxy_sessions, p0[0]);
2036	      if (PREDICT_FALSE(proxy0 == 0))
2037		{
2038		  error0 = NSH_NODE_ERROR_NO_PROXY;
2039		  goto trace00;
2040		}
2041	      nsp_nsi0 = proxy0->nsp_nsi;
2042	    }
2043
2044	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
2045
2046	  if (PREDICT_FALSE(entry0 == 0))
2047	    {
2048	      error0 = NSH_NODE_ERROR_NO_MAPPING;
2049	      goto trace00;
2050	    }
2051
2052	  /* Entry should point to a mapping ...*/
2053	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
2054
2055	  if (PREDICT_FALSE(map0 == 0))
2056	    {
2057	      error0 = NSH_NODE_ERROR_NO_MAPPING;
2058	      goto trace00;
2059	    }
2060
2061	  /* set up things for next node to transmit ie which node to handle it and where */
2062	  next0 = map0->next_node;
2063	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
2064
2065	  if(PREDICT_FALSE(map0->nsh_action == NSH_ACTION_POP))
2066	    {
2067	      /* Manipulate MD2 */
2068              if(PREDICT_FALSE(hdr0->md_type == 2))
2069        	{
2070        	  nsh_md2_decap(b0, hdr0, &header_len0, &next0, NSH_NODE_NEXT_DROP);
2071        	  if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
2072        	    {
2073        	      error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
2074        	      goto trace00;
2075        	    }
2076	          vnet_buffer(b0)->sw_if_index[VLIB_RX] = map0->rx_sw_if_index;
2077        	}
2078
2079              /* Pop NSH header */
2080	      vlib_buffer_advance(b0, (word)header_len0);
2081	      goto trace00;
2082	    }
2083
2084	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
2085	  if (PREDICT_FALSE(entry0 == 0))
2086	    {
2087	      error0 = NSH_NODE_ERROR_NO_ENTRY;
2088	      goto trace00;
2089	    }
2090
2091	  nsh_entry0 = (nsh_entry_t *)pool_elt_at_index(nm->nsh_entries, entry0[0]);
2092	  encap_hdr0 = (nsh_base_header_t *)(nsh_entry0->rewrite);
2093	  /* rewrite_size should equal to (encap_hdr0->length * 4) */
2094	  encap_hdr_len0 = nsh_entry0->rewrite_size;
2095
2096	  if(PREDICT_TRUE(map0->nsh_action == NSH_ACTION_SWAP))
2097	    {
2098	      /* Manipulate MD2 */
2099              if(PREDICT_FALSE(hdr0->md_type == 2))
2100        	{
2101        	  nsh_md2_swap(b0, hdr0, header_len0, nsh_entry0,
2102        	               &next0, NSH_NODE_NEXT_DROP);
2103        	  if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
2104        	    {
2105        	      error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
2106        	      goto trace00;
2107        	    }
2108        	}
2109
2110              /* Pop old NSH header */
2111	      vlib_buffer_advance(b0, (word)header_len0);
2112
2113              /* After processing, md2's length may be varied */
2114              encap_hdr_len0 = nsh_entry0->rewrite_size;
2115	      /* Push new NSH header */
2116	      vlib_buffer_advance(b0, -(word)encap_hdr_len0);
2117	      hdr0 = vlib_buffer_get_current(b0);
2118	      clib_memcpy(hdr0, encap_hdr0, (word)encap_hdr_len0);
2119
2120	      goto trace00;
2121	    }
2122
2123	  if(PREDICT_TRUE(map0->nsh_action == NSH_ACTION_PUSH))
2124	    {
2125              /* After processing, md2's length may be varied */
2126              encap_hdr_len0 = nsh_entry0->rewrite_size;
2127	      /* Push new NSH header */
2128	      vlib_buffer_advance(b0, -(word)encap_hdr_len0);
2129	      hdr0 = vlib_buffer_get_current(b0);
2130	      clib_memcpy(hdr0, encap_hdr0, (word)encap_hdr_len0);
2131	      /* Manipulate MD2 */
2132              if(PREDICT_FALSE(nsh_entry0->nsh_base.md_type == 2))
2133        	{
2134        	  nsh_md2_encap(b0, hdr0, nsh_entry0);
2135        	}
2136
2137	    }
2138
2139	  trace00: b0->error = error0 ? node->errors[error0] : 0;
2140
2141	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2142	    {
2143	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
2144	      clib_memcpy ( &(tr->trace_data[0]), hdr0, (hdr0->length*4) );
2145	    }
2146
2147	  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
2148					  n_left_to_next, bi0, next0);
2149	}
2150
2151      vlib_put_next_frame(vm, node, next_index, n_left_to_next);
2152
2153    }
2154
2155  return from_frame->n_vectors;
2156}
2157
2158/**
2159 * @brief Graph processing dispatch function for NSH Input
2160 *
2161 * @node nsh_input
2162 * @param *vm
2163 * @param *node
2164 * @param *from_frame
2165 *
2166 * @return from_frame->n_vectors
2167 *
2168 */
2169static uword
2170nsh_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2171                  vlib_frame_t * from_frame)
2172{
2173  return nsh_input_map (vm, node, from_frame, NSH_INPUT_TYPE);
2174}
2175
2176/**
2177 * @brief Graph processing dispatch function for NSH-Proxy
2178 *
2179 * @node nsh_proxy
2180 * @param *vm
2181 * @param *node
2182 * @param *from_frame
2183 *
2184 * @return from_frame->n_vectors
2185 *
2186 */
2187static uword
2188nsh_proxy (vlib_main_t * vm, vlib_node_runtime_t * node,
2189                  vlib_frame_t * from_frame)
2190{
2191  return nsh_input_map (vm, node, from_frame, NSH_PROXY_TYPE);
2192}
2193
2194/**
2195 * @brief Graph processing dispatch function for NSH Classifier
2196 *
2197 * @node nsh_classifier
2198 * @param *vm
2199 * @param *node
2200 * @param *from_frame
2201 *
2202 * @return from_frame->n_vectors
2203 *
2204 */
2205static uword
2206nsh_classifier (vlib_main_t * vm, vlib_node_runtime_t * node,
2207                  vlib_frame_t * from_frame)
2208{
2209  return nsh_input_map (vm, node, from_frame, NSH_CLASSIFIER_TYPE);
2210}
2211
2212/**
2213 * @brief Graph processing dispatch function for NSH-AWARE-VNF-PROXY
2214 *
2215 * @node nsh_aware_vnf_proxy
2216 * @param *vm
2217 * @param *node
2218 * @param *from_frame
2219 *
2220 * @return from_frame->n_vectors
2221 *
2222 */
2223static uword
2224nsh_aware_vnf_proxy (vlib_main_t * vm, vlib_node_runtime_t * node,
2225                     vlib_frame_t * from_frame)
2226{
2227  return nsh_input_map (vm, node, from_frame, NSH_AWARE_VNF_PROXY_TYPE);
2228}
2229
2230static char * nsh_node_error_strings[] = {
2231#define _(sym,string) string,
2232  foreach_nsh_node_error
2233#undef _
2234};
2235
2236/* register nsh-input node */
2237VLIB_REGISTER_NODE (nsh_input_node) = {
2238  .function = nsh_input,
2239  .name = "nsh-input",
2240  .vector_size = sizeof (u32),
2241  .format_trace = format_nsh_node_map_trace,
2242  .format_buffer = format_nsh_header,
2243  .type = VLIB_NODE_TYPE_INTERNAL,
2244
2245  .n_errors = ARRAY_LEN(nsh_node_error_strings),
2246  .error_strings = nsh_node_error_strings,
2247
2248  .n_next_nodes = NSH_NODE_N_NEXT,
2249
2250  .next_nodes = {
2251#define _(s,n) [NSH_NODE_NEXT_##s] = n,
2252    foreach_nsh_node_next
2253#undef _
2254  },
2255};
2256
2257VLIB_NODE_FUNCTION_MULTIARCH (nsh_input_node, nsh_input);
2258
2259/* register nsh-proxy node */
2260VLIB_REGISTER_NODE (nsh_proxy_node) = {
2261  .function = nsh_proxy,
2262  .name = "nsh-proxy",
2263  .vector_size = sizeof (u32),
2264  .format_trace = format_nsh_node_map_trace,
2265  .format_buffer = format_nsh_header,
2266  .type = VLIB_NODE_TYPE_INTERNAL,
2267
2268  .n_errors = ARRAY_LEN(nsh_node_error_strings),
2269  .error_strings = nsh_node_error_strings,
2270
2271  .n_next_nodes = NSH_NODE_N_NEXT,
2272
2273  .next_nodes = {
2274#define _(s,n) [NSH_NODE_NEXT_##s] = n,
2275    foreach_nsh_node_next
2276#undef _
2277  },
2278};
2279
2280VLIB_NODE_FUNCTION_MULTIARCH (nsh_proxy_node, nsh_proxy);
2281
2282/* register nsh-classifier node */
2283VLIB_REGISTER_NODE (nsh_classifier_node) = {
2284  .function = nsh_classifier,
2285  .name = "nsh-classifier",
2286  .vector_size = sizeof (u32),
2287  .format_trace = format_nsh_node_map_trace,
2288  .format_buffer = format_nsh_header,
2289  .type = VLIB_NODE_TYPE_INTERNAL,
2290
2291  .n_errors = ARRAY_LEN(nsh_node_error_strings),
2292  .error_strings = nsh_node_error_strings,
2293
2294  .n_next_nodes = NSH_NODE_N_NEXT,
2295
2296  .next_nodes = {
2297#define _(s,n) [NSH_NODE_NEXT_##s] = n,
2298    foreach_nsh_node_next
2299#undef _
2300  },
2301};
2302
2303VLIB_NODE_FUNCTION_MULTIARCH (nsh_classifier_node, nsh_classifier);
2304
2305/* register nsh-aware-vnf-proxy node */
2306VLIB_REGISTER_NODE (nsh_aware_vnf_proxy_node) = {
2307  .function = nsh_aware_vnf_proxy,
2308  .name = "nsh-aware-vnf-proxy",
2309  .vector_size = sizeof (u32),
2310  .format_trace = format_nsh_node_map_trace,
2311  .format_buffer = format_nsh_header,
2312  .type = VLIB_NODE_TYPE_INTERNAL,
2313
2314  .n_errors = ARRAY_LEN(nsh_node_error_strings),
2315  .error_strings = nsh_node_error_strings,
2316
2317  .n_next_nodes = NSH_NODE_N_NEXT,
2318
2319  .next_nodes = {
2320#define _(s,n) [NSH_NODE_NEXT_##s] = n,
2321    foreach_nsh_node_next
2322#undef _
2323  },
2324};
2325
2326VLIB_NODE_FUNCTION_MULTIARCH (nsh_aware_vnf_proxy_node, nsh_aware_vnf_proxy);
2327
2328void
2329nsh_md2_set_next_ioam_export_override (uword next)
2330{
2331  nsh_main_t *hm = &nsh_main;
2332  hm->decap_v4_next_override = next;
2333  return;
2334}
2335
2336
2337clib_error_t *nsh_init (vlib_main_t *vm)
2338{
2339  nsh_main_t *nm = &nsh_main;
2340  clib_error_t * error = 0;
2341  u8 * name;
2342  uword next_node;
2343
2344  /* Init the main structures from VPP */
2345  nm->vlib_main = vm;
2346  nm->vnet_main = vnet_get_main();
2347
2348  /* Various state maintenance mappings */
2349  nm->nsh_mapping_by_key
2350    = hash_create_mem (0, sizeof(u32), sizeof (uword));
2351
2352  nm->nsh_mapping_by_mapped_key
2353    = hash_create_mem (0, sizeof(u32), sizeof (uword));
2354
2355  nm->nsh_entry_by_key
2356    = hash_create_mem (0, sizeof(u32), sizeof (uword));
2357
2358  nm->nsh_proxy_session_by_key
2359    = hash_create_mem (0, sizeof(nsh_proxy_session_by_key_t), sizeof (uword));
2360
2361  nm->nsh_option_map_by_key
2362    = hash_create_mem (0, sizeof(nsh_option_map_by_key_t), sizeof (uword));
2363
2364  name = format (0, "nsh_%08x%c", api_version, 0);
2365
2366  /* Set up the API */
2367  nm->msg_id_base = vl_msg_api_get_msg_ids
2368    ((char *) name, VL_MSG_FIRST_AVAILABLE);
2369
2370  error = nsh_plugin_api_hookup (vm);
2371
2372  /* Add our API messages to the global name_crc hash table */
2373  setup_message_id_table (nm, &api_main);
2374
2375  /* Add dispositions to nodes that feed nsh-input */
2376  //alagalah - validate we don't really need to use the node value
2377  next_node = vlib_node_add_next (vm, vxlan4_gpe_input_node.index, nsh_input_node.index);
2378  vlib_node_add_next (vm, vxlan4_gpe_input_node.index, nsh_proxy_node.index);
2379  vlib_node_add_next (vm, vxlan4_gpe_input_node.index, nsh_aware_vnf_proxy_node.index);
2380  vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_NSH, next_node);
2381
2382  vlib_node_add_next (vm, vxlan6_gpe_input_node.index, nsh_input_node.index);
2383  vlib_node_add_next (vm, vxlan6_gpe_input_node.index, nsh_proxy_node.index);
2384  vlib_node_add_next (vm, vxlan6_gpe_input_node.index, nsh_aware_vnf_proxy_node.index);
2385
2386  vlib_node_add_next (vm, gre4_input_node.index, nsh_input_node.index);
2387  vlib_node_add_next (vm, gre4_input_node.index, nsh_proxy_node.index);
2388  vlib_node_add_next (vm, gre4_input_node.index, nsh_aware_vnf_proxy_node.index);
2389
2390  vlib_node_add_next (vm, gre6_input_node.index, nsh_input_node.index);
2391  vlib_node_add_next (vm, gre6_input_node.index, nsh_proxy_node.index);
2392  vlib_node_add_next (vm, gre6_input_node.index, nsh_aware_vnf_proxy_node.index);
2393
2394  /* Add NSH-Proxy support */
2395  vlib_node_add_next (vm, vxlan4_input_node.index, nsh_proxy_node.index);
2396  vlib_node_add_next (vm, vxlan6_input_node.index, nsh_proxy_node.index);
2397
2398  /* Add NSH-Classifier support */
2399  vlib_node_add_next (vm, ip4_classify_node.index, nsh_classifier_node.index);
2400  vlib_node_add_next (vm, ip6_classify_node.index, nsh_classifier_node.index);
2401  vlib_node_add_next (vm, l2_input_classify_node.index, nsh_classifier_node.index);
2402
2403  vec_free(name);
2404
2405  return error;
2406}
2407
2408VLIB_INIT_FUNCTION(nsh_init);
2409