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