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