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