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