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