nsh.c revision 37a5ce7b
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
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vlibsocket/api.h>
25
26/* define message IDs */
27#include <vpp-api/nsh_msg_enum.h>
28
29/* define message structures */
30#define vl_typedefs
31#include <vpp-api/nsh_all_api_h.h>
32#undef vl_typedefs
33
34/* define generated endian-swappers */
35#define vl_endianfun
36#include <vpp-api/nsh_all_api_h.h>
37#undef vl_endianfun
38
39/* instantiate all the print functions we know about */
40#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41#define vl_printfun
42#include <vpp-api/nsh_all_api_h.h>
43#undef vl_printfun
44
45/* Get the API version number */
46#define vl_api_version(n,v) static u32 api_version=(v);
47#include <vpp-api/nsh_all_api_h.h>
48#undef vl_api_version
49
50/*
51 * A handy macro to set up a message reply.
52 * Assumes that the following variables are available:
53 * mp - pointer to request message
54 * rmp - pointer to reply message type
55 * rv - return value
56 */
57
58#define REPLY_MACRO(t)                                          \
59  do {								\
60    unix_shared_memory_queue_t * q =                            \
61      vl_api_client_index_to_input_queue (mp->client_index);	\
62    if (!q)                                                     \
63      return;							\
64                                                                \
65    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
66    rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base);               \
67    rmp->context = mp->context;                                 \
68    rmp->retval = ntohl(rv);                                    \
69                                                                \
70    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
71  } while(0);
72
73#define REPLY_MACRO2(t, body)                                   \
74  do {                                                          \
75    unix_shared_memory_queue_t * q;                             \
76    rv = vl_msg_api_pd_handler (mp, rv);                        \
77    q = vl_api_client_index_to_input_queue (mp->client_index);  \
78    if (!q)                                                     \
79        return;                                                 \
80                                                                \
81    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
82    rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base);               \
83    rmp->context = mp->context;                                 \
84    rmp->retval = ntohl(rv);                                    \
85    do {body;} while (0);                                       \
86    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
87  } while(0);
88
89#define FINISH                                  \
90    vec_add1 (s, 0);                            \
91    vl_print (handle, (char *)s);               \
92    vec_free (s);                               \
93    return handle;
94
95/* List of message types that this plugin understands */
96
97#define foreach_nsh_plugin_api_msg		\
98  _(NSH_ADD_DEL_ENTRY, nsh_add_del_entry)	\
99  _(NSH_ENTRY_DUMP, nsh_entry_dump)             \
100  _(NSH_ADD_DEL_MAP, nsh_add_del_map)           \
101  _(NSH_MAP_DUMP, nsh_map_dump)
102
103clib_error_t *
104vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
105                      int from_early_init)
106{
107  nsh_main_t * nm = &nsh_main;
108  clib_error_t * error = 0;
109
110  nm->vlib_main = vm;
111  nm->vnet_main = h->vnet_main;
112
113  return error;
114}
115
116typedef struct {
117  nsh_header_t nsh_header;
118} nsh_input_trace_t;
119
120u8 * format_nsh_header (u8 * s, va_list * args)
121{
122  nsh_header_t * nsh = va_arg (*args, nsh_header_t *);
123
124  s = format (s, "nsh ver %d ", (nsh->ver_o_c>>6));
125  if (nsh->ver_o_c & NSH_O_BIT)
126    s = format (s, "O-set ");
127
128  if (nsh->ver_o_c & NSH_C_BIT)
129    s = format (s, "C-set ");
130
131  s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
132              nsh->length, nsh->length * 4, nsh->md_type, nsh->next_protocol);
133
134  s = format (s, "  service path %d service index %d\n",
135              (nsh->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
136              nsh->nsp_nsi & NSH_NSI_MASK);
137
138  s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
139              nsh->c1, nsh->c2, nsh->c3, nsh->c4);
140
141  return s;
142}
143
144static u8 * format_nsh_action (u8 * s, va_list * args)
145{
146  u32 nsh_action = va_arg (*args, u32);
147
148  switch (nsh_action)
149    {
150    case NSH_ACTION_SWAP:
151      return format (s, "swap");
152    case NSH_ACTION_PUSH:
153      return format (s, "push");
154    case NSH_ACTION_POP:
155      return format (s, "pop");
156    default:
157      return format (s, "unknown %d", nsh_action);
158    }
159  return s;
160}
161
162u8 * format_nsh_map (u8 * s, va_list * args)
163{
164  nsh_map_t * map = va_arg (*args, nsh_map_t *);
165
166  s = format (s, "nsh entry nsp: %d nsi: %d ",
167              (map->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
168              map->nsp_nsi & NSH_NSI_MASK);
169  s = format (s, "maps to nsp: %d nsi: %d ",
170              (map->mapped_nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
171              map->mapped_nsp_nsi & NSH_NSI_MASK);
172
173  s = format (s, " nsh_action %U\n", format_nsh_action, map->nsh_action);
174
175  switch (map->next_node)
176    {
177    case NSH_INPUT_NEXT_ENCAP_GRE:
178      {
179	s = format (s, "encapped by GRE intf: %d", map->sw_if_index);
180	break;
181      }
182    case NSH_INPUT_NEXT_ENCAP_VXLANGPE:
183      {
184	s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
185	break;
186      }
187    default:
188      s = format (s, "only GRE and VXLANGPE support in this rev");
189    }
190
191  return s;
192}
193
194u8 * format_nsh_header_with_length (u8 * s, va_list * args)
195{
196  nsh_header_t * h = va_arg (*args, nsh_header_t *);
197  u32 max_header_bytes = va_arg (*args, u32);
198  u32 tmp, header_bytes;
199
200  header_bytes = sizeof (h[0]);
201  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
202    return format (s, "nsh header truncated");
203
204  tmp = clib_net_to_host_u32 (h->nsp_nsi);
205  s = format (s, "  nsp %d nsi %d ",
206              (tmp>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
207              tmp & NSH_NSI_MASK);
208
209  s = format (s, "c1 %u c2 %u c3 %u c4 %u",
210              clib_net_to_host_u32 (h->c1),
211              clib_net_to_host_u32 (h->c2),
212              clib_net_to_host_u32 (h->c3),
213              clib_net_to_host_u32 (h->c4));
214
215  s = format (s, "ver %d ", h->ver_o_c>>6);
216
217  if (h->ver_o_c & NSH_O_BIT)
218    s = format (s, "O-set ");
219
220  if (h->ver_o_c & NSH_C_BIT)
221    s = format (s, "C-set ");
222
223  s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
224              h->length, h->length * 4, h->md_type, h->next_protocol);
225  return s;
226}
227
228u8 * format_nsh_input_map_trace (u8 * s, va_list * args)
229{
230  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
231  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
232  nsh_input_trace_t * t
233    = va_arg (*args, nsh_input_trace_t *);
234
235  s = format (s, "\n  %U", format_nsh_header, &t->nsh_header,
236              (u32) sizeof (t->nsh_header) );
237
238  return s;
239}
240
241/**
242 * Action function to add or del an nsh map.
243 * Shared by both CLI and binary API
244 **/
245
246int nsh_add_del_map (nsh_add_del_map_args_t *a, u32 * map_indexp)
247{
248  nsh_main_t * nm = &nsh_main;
249  nsh_map_t *map = 0;
250  u32 key, *key_copy;
251  uword * entry;
252  hash_pair_t *hp;
253  u32 map_index = ~0;
254
255  key = a->map.nsp_nsi;
256
257  entry = hash_get_mem (nm->nsh_mapping_by_key, &key);
258
259  if (a->is_add)
260    {
261      /* adding an entry, must not already exist */
262      if (entry)
263        return -1; //TODO API_ERROR_INVALID_VALUE;
264
265      pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES);
266      memset (map, 0, sizeof (*map));
267
268      /* copy from arg structure */
269      map->nsp_nsi = a->map.nsp_nsi;
270      map->mapped_nsp_nsi = a->map.mapped_nsp_nsi;
271      map->nsh_action = a->map.nsh_action;
272      map->sw_if_index = a->map.sw_if_index;
273      map->next_node = a->map.next_node;
274
275
276      key_copy = clib_mem_alloc (sizeof (*key_copy));
277      clib_memcpy (key_copy, &key, sizeof (*key_copy));
278
279      hash_set_mem (nm->nsh_mapping_by_key, key_copy,
280                    map - nm->nsh_mappings);
281      map_index = map - nm->nsh_mappings;
282    }
283  else
284    {
285      if (!entry)
286	return -2 ; //TODO API_ERROR_NO_SUCH_ENTRY;
287
288      map = pool_elt_at_index (nm->nsh_mappings, entry[0]);
289      hp = hash_get_pair (nm->nsh_mapping_by_key, &key);
290      key_copy = (void *)(hp->key);
291      hash_unset_mem (nm->nsh_mapping_by_key, &key);
292      clib_mem_free (key_copy);
293
294      pool_put (nm->nsh_mappings, map);
295    }
296
297  if (map_indexp)
298      *map_indexp = map_index;
299
300  return 0;
301}
302
303/**
304 * CLI command for NSH map
305 */
306
307static uword unformat_nsh_action (unformat_input_t * input, va_list * args)
308{
309  u32 * result = va_arg (*args, u32 *);
310  u32 tmp;
311
312  if (unformat (input, "swap"))
313    *result = NSH_ACTION_SWAP;
314  if (unformat (input, "push"))
315    *result = NSH_ACTION_PUSH;
316  if (unformat (input, "pop"))
317    *result = NSH_ACTION_POP;
318  else if (unformat (input, "%d", &tmp))
319    *result = tmp;
320  else
321    return 0;
322  return 1;
323}
324
325static clib_error_t *
326nsh_add_del_map_command_fn (vlib_main_t * vm,
327			    unformat_input_t * input,
328			    vlib_cli_command_t * cmd)
329{
330  unformat_input_t _line_input, * line_input = &_line_input;
331  u8 is_add = 1;
332  u32 nsp, nsi, mapped_nsp, mapped_nsi, nsh_action;
333  int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
334  int nsh_action_set = 0;
335  u32 next_node = ~0;
336  u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
337  nsh_add_del_map_args_t _a, * a = &_a;
338  u32 map_index;
339  int rv;
340
341  /* Get a line of input. */
342  if (! unformat_user (input, unformat_line_input, line_input))
343    return 0;
344
345  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
346    if (unformat (line_input, "del"))
347      is_add = 0;
348    else if (unformat (line_input, "nsp %d", &nsp))
349      nsp_set = 1;
350    else if (unformat (line_input, "nsi %d", &nsi))
351      nsi_set = 1;
352    else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
353      mapped_nsp_set = 1;
354    else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
355      mapped_nsi_set = 1;
356    else if (unformat (line_input, "nsh_action %U", unformat_nsh_action,
357                       &nsh_action))
358      nsh_action_set = 1;
359    else if (unformat (line_input, "encap-gre-intf %d", &sw_if_index))
360      next_node = NSH_INPUT_NEXT_ENCAP_GRE;
361    else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
362      next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE;
363    else if (unformat (line_input, "encap-none"))
364      next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
365    else
366      return clib_error_return (0, "parse error: '%U'",
367                                format_unformat_error, line_input);
368  }
369
370  unformat_free (line_input);
371
372  if (nsp_set == 0 || nsi_set == 0)
373    return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
374
375  if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
376    return clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
377
378  if (nsh_action_set == 0 )
379    return clib_error_return (0, "nsh_action required: swap|push|pop.");
380
381  if (next_node == ~0)
382    return clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
383
384  memset (a, 0, sizeof (*a));
385
386  /* set args structure */
387  a->is_add = is_add;
388  a->map.nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
389  a->map.mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
390  a->map.nsh_action = nsh_action;
391  a->map.sw_if_index = sw_if_index;
392  a->map.next_node = next_node;
393
394
395  rv = nsh_add_del_map (a, &map_index);
396
397  switch(rv)
398    {
399    case 0:
400      break;
401    case -1: //TODO API_ERROR_INVALID_VALUE:
402      return clib_error_return (0, "mapping already exists. Remove it first.");
403
404    case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
405      return clib_error_return (0, "mapping does not exist.");
406
407    default:
408      return clib_error_return
409        (0, "nsh_add_del_map returned %d", rv);
410    }
411  return 0;
412}
413
414VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
415  .path = "create nsh map",
416  .short_help =
417  "create nsh map nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> nsh_action [swap|push|pop] "
418  "[encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]\n",
419  .function = nsh_add_del_map_command_fn,
420};
421
422/** API message handler */
423static void vl_api_nsh_add_del_map_t_handler
424(vl_api_nsh_add_del_map_t * mp)
425{
426  vl_api_nsh_add_del_map_reply_t * rmp;
427  nsh_main_t * nm = &nsh_main;
428  int rv;
429  nsh_add_del_map_args_t _a, *a = &_a;
430  u32 map_index = ~0;
431
432  a->is_add = mp->is_add;
433  a->map.nsp_nsi = ntohl(mp->nsp_nsi);
434  a->map.mapped_nsp_nsi = ntohl(mp->mapped_nsp_nsi);
435  a->map.nsh_action = ntohl(mp->nsh_action);
436  a->map.sw_if_index = ntohl(mp->sw_if_index);
437  a->map.next_node = ntohl(mp->next_node);
438
439  rv = nsh_add_del_map (a, &map_index);
440
441  REPLY_MACRO2(VL_API_NSH_ADD_DEL_MAP_REPLY,
442  ({
443    rmp->map_index = htonl (map_index);
444  }));
445}
446
447/**
448 * CLI command for showing the mapping between NSH entries
449 */
450static clib_error_t *
451show_nsh_map_command_fn (vlib_main_t * vm,
452			 unformat_input_t * input,
453			 vlib_cli_command_t * cmd)
454{
455  nsh_main_t * nm = &nsh_main;
456  nsh_map_t * map;
457
458  if (pool_elts (nm->nsh_mappings) == 0)
459    vlib_cli_output (vm, "No nsh maps configured.");
460
461  pool_foreach (map, nm->nsh_mappings,
462		({
463		  vlib_cli_output (vm, "%U", format_nsh_map, map);
464		}));
465
466  return 0;
467}
468
469VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
470  .path = "show nsh map",
471  .function = show_nsh_map_command_fn,
472};
473
474/**
475 * Action function for adding an NSH entry
476 */
477
478int nsh_add_del_entry (nsh_add_del_entry_args_t *a, u32 * entry_indexp)
479{
480  nsh_main_t * nm = &nsh_main;
481  nsh_header_t *hdr = 0;
482  u32 key, *key_copy;
483  uword * entry;
484  hash_pair_t *hp;
485  u32 entry_index = ~0;
486
487  key = a->nsh.nsp_nsi;
488
489  entry = hash_get_mem (nm->nsh_entry_by_key, &key);
490
491  if (a->is_add)
492    {
493      /* adding an entry, must not already exist */
494      if (entry)
495        return -1; // TODO VNET_API_ERROR_INVALID_VALUE;
496
497      pool_get_aligned (nm->nsh_entries, hdr, CLIB_CACHE_LINE_BYTES);
498      memset (hdr, 0, sizeof (*hdr));
499
500      /* copy from arg structure */
501#define _(x) hdr->x = a->nsh.x;
502      foreach_copy_nshhdr_field;
503#undef _
504
505      key_copy = clib_mem_alloc (sizeof (*key_copy));
506      clib_memcpy (key_copy, &key, sizeof (*key_copy));
507
508      hash_set_mem (nm->nsh_entry_by_key, key_copy,
509                    hdr - nm->nsh_entries);
510      entry_index = hdr - nm->nsh_entries;
511    }
512  else
513    {
514      if (!entry)
515	return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
516
517      hdr = pool_elt_at_index (nm->nsh_entries, entry[0]);
518      hp = hash_get_pair (nm->nsh_entry_by_key, &key);
519      key_copy = (void *)(hp->key);
520      hash_unset_mem (nm->nsh_entry_by_key, &key);
521      clib_mem_free (key_copy);
522
523      pool_put (nm->nsh_entries, hdr);
524    }
525
526  if (entry_indexp)
527      *entry_indexp = entry_index;
528
529  return 0;
530}
531
532
533/**
534 * CLI command for adding NSH entry
535 */
536
537static clib_error_t *
538nsh_add_del_entry_command_fn (vlib_main_t * vm,
539			      unformat_input_t * input,
540			      vlib_cli_command_t * cmd)
541{
542  unformat_input_t _line_input, * line_input = &_line_input;
543  u8 is_add = 1;
544  u8 ver_o_c = 0;
545  u8 length = 0;
546  u8 md_type = 0;
547  u8 next_protocol = 1; /* default: ip4 */
548  u32 nsp;
549  u8 nsp_set = 0;
550  u32 nsi;
551  u8 nsi_set = 0;
552  u32 nsp_nsi;
553  u32 c1 = 0;
554  u32 c2 = 0;
555  u32 c3 = 0;
556  u32 c4 = 0;
557  u32 tmp;
558  int rv;
559  u32 entry_index;
560  nsh_add_del_entry_args_t _a, * a = &_a;
561
562  /* Get a line of input. */
563  if (! unformat_user (input, unformat_line_input, line_input))
564    return 0;
565
566  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
567    if (unformat (line_input, "del"))
568      is_add = 0;
569    else if (unformat (line_input, "version %d", &tmp))
570      ver_o_c |= (tmp & 3) << 6;
571    else if (unformat (line_input, "o-bit %d", &tmp))
572      ver_o_c |= (tmp & 1) << 5;
573    else if (unformat (line_input, "c-bit %d", &tmp))
574      ver_o_c |= (tmp & 1) << 4;
575    else if (unformat (line_input, "md-type %d", &tmp))
576      md_type = tmp;
577    else if (unformat(line_input, "next-ip4"))
578      next_protocol = 1;
579    else if (unformat(line_input, "next-ip6"))
580      next_protocol = 2;
581    else if (unformat(line_input, "next-ethernet"))
582      next_protocol = 3;
583    else if (unformat (line_input, "c1 %d", &c1))
584      ;
585    else if (unformat (line_input, "c2 %d", &c2))
586      ;
587    else if (unformat (line_input, "c3 %d", &c3))
588      ;
589    else if (unformat (line_input, "c4 %d", &c4))
590      ;
591    else if (unformat (line_input, "nsp %d", &nsp))
592      nsp_set = 1;
593    else if (unformat (line_input, "nsi %d", &nsi))
594      nsi_set = 1;
595    else
596      return clib_error_return (0, "parse error: '%U'",
597                                format_unformat_error, line_input);
598  }
599
600  unformat_free (line_input);
601
602  if (nsp_set == 0)
603    return clib_error_return (0, "nsp not specified");
604
605  if (nsi_set == 0)
606    return clib_error_return (0, "nsi not specified");
607
608  if (md_type != 1)
609    return clib_error_return (0, "md-type 1 only supported at this time");
610
611  md_type = 1;
612  length = 6;
613
614  nsp_nsi = (nsp<<8) | nsi;
615
616  memset (a, 0, sizeof (*a));
617
618  a->is_add = is_add;
619
620#define _(x) a->nsh.x = x;
621  foreach_copy_nshhdr_field;
622#undef _
623
624  rv = nsh_add_del_entry (a, &entry_index);
625
626  switch(rv)
627    {
628    case 0:
629      break;
630    default:
631      return clib_error_return
632        (0, "nsh_add_del_entry returned %d", rv);
633    }
634
635  return 0;
636}
637
638VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
639  .path = "create nsh entry",
640  .short_help =
641  "create nsh entry {nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn>"
642  " [md-type <nn>] [tlv <xx>] [del]\n",
643  .function = nsh_add_del_entry_command_fn,
644};
645
646/** API message handler */
647static void vl_api_nsh_add_del_entry_t_handler
648(vl_api_nsh_add_del_entry_t * mp)
649{
650  vl_api_nsh_add_del_entry_reply_t * rmp;
651  nsh_main_t * nm = &nsh_main;
652  int rv;
653  nsh_add_del_entry_args_t _a, *a = &_a;
654  u32 entry_index = ~0;
655
656  a->is_add = mp->is_add;
657  a->nsh.ver_o_c = mp->ver_o_c;
658  a->nsh.length = mp->length;
659  a->nsh.md_type = mp->md_type;
660  a->nsh.next_protocol = mp->next_protocol;
661  a->nsh.nsp_nsi = ntohl(mp->nsp_nsi);
662  a->nsh.c1 = ntohl(mp->c1);
663  a->nsh.c2 = ntohl(mp->c2);
664  a->nsh.c3 = ntohl(mp->c3);
665  a->nsh.c4 = ntohl(mp->c4);
666
667  rv = nsh_add_del_entry (a, &entry_index);
668
669  REPLY_MACRO2(VL_API_NSH_ADD_DEL_ENTRY_REPLY,
670  ({
671    rmp->entry_index = htonl (entry_index);
672  }));
673}
674
675static void send_nsh_entry_details
676(nsh_header_t * t, unix_shared_memory_queue_t * q, u32 context)
677{
678    vl_api_nsh_entry_details_t * rmp;
679    nsh_main_t * nm = &nsh_main;
680
681    rmp = vl_msg_api_alloc (sizeof (*rmp));
682    memset (rmp, 0, sizeof (*rmp));
683
684    rmp->_vl_msg_id = ntohs((VL_API_NSH_ENTRY_DETAILS)+nm->msg_id_base);
685    rmp->ver_o_c = t->ver_o_c;
686    rmp->length = t->length;
687    rmp->md_type = t->md_type;
688    rmp->next_protocol = t->next_protocol;
689    rmp->nsp_nsi = htonl(t->nsp_nsi);
690    rmp->c1 = htonl(t->c1);
691    rmp->c2 = htonl(t->c2);
692    rmp->c3 = htonl(t->c3);
693    rmp->c4 = htonl(t->c4);
694
695    rmp->context = context;
696
697    vl_msg_api_send_shmem (q, (u8 *)&rmp);
698}
699
700static void vl_api_nsh_entry_dump_t_handler
701(vl_api_nsh_entry_dump_t * mp)
702{
703    unix_shared_memory_queue_t * q;
704    nsh_main_t * nm = &nsh_main;
705    nsh_header_t * t;
706    u32 entry_index;
707
708    q = vl_api_client_index_to_input_queue (mp->client_index);
709    if (q == 0) {
710        return;
711    }
712
713    entry_index = ntohl (mp->entry_index);
714
715    if (~0 == entry_index)
716      {
717	pool_foreach (t, nm->nsh_entries,
718	({
719	    send_nsh_entry_details(t, q, mp->context);
720	}));
721      }
722    else
723      {
724        if (entry_index >= vec_len (nm->nsh_entries))
725  	{
726  	  return;
727  	}
728        t = &nm->nsh_entries[entry_index];
729        send_nsh_entry_details(t, q, mp->context);
730      }
731}
732
733static void send_nsh_map_details
734(nsh_map_t * t, unix_shared_memory_queue_t * q, u32 context)
735{
736    vl_api_nsh_map_details_t * rmp;
737    nsh_main_t * nm = &nsh_main;
738
739    rmp = vl_msg_api_alloc (sizeof (*rmp));
740    memset (rmp, 0, sizeof (*rmp));
741
742    rmp->_vl_msg_id = ntohs((VL_API_NSH_MAP_DETAILS)+nm->msg_id_base);
743    rmp->nsp_nsi = htonl(t->nsp_nsi);
744    rmp->mapped_nsp_nsi = htonl(t->mapped_nsp_nsi);
745    rmp->nsh_action = htonl(t->nsh_action);
746    rmp->sw_if_index = htonl(t->sw_if_index);
747    rmp->next_node = htonl(t->next_node);
748
749    rmp->context = context;
750
751    vl_msg_api_send_shmem (q, (u8 *)&rmp);
752}
753
754static void vl_api_nsh_map_dump_t_handler
755(vl_api_nsh_map_dump_t * mp)
756{
757    unix_shared_memory_queue_t * q;
758    nsh_main_t * nm = &nsh_main;
759    nsh_map_t * t;
760    u32 map_index;
761
762    q = vl_api_client_index_to_input_queue (mp->client_index);
763    if (q == 0) {
764        return;
765    }
766
767    map_index = ntohl (mp->map_index);
768
769    if (~0 == map_index)
770      {
771	pool_foreach (t, nm->nsh_mappings,
772	({
773	    send_nsh_map_details(t, q, mp->context);
774	}));
775      }
776    else
777      {
778        if (map_index >= vec_len (nm->nsh_mappings))
779  	{
780  	  return;
781  	}
782        t = &nm->nsh_mappings[map_index];
783        send_nsh_map_details(t, q, mp->context);
784      }
785}
786
787static clib_error_t *
788show_nsh_entry_command_fn (vlib_main_t * vm,
789			   unformat_input_t * input,
790			   vlib_cli_command_t * cmd)
791{
792  nsh_main_t * nm = &nsh_main;
793  nsh_header_t * hdr;
794
795  if (pool_elts (nm->nsh_entries) == 0)
796    vlib_cli_output (vm, "No nsh entries configured.");
797
798  pool_foreach (hdr, nm->nsh_entries,
799		({
800		  vlib_cli_output (vm, "%U", format_nsh_header, hdr);
801		}));
802
803  return 0;
804}
805
806VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
807  .path = "show nsh entry",
808  .function = show_nsh_entry_command_fn,
809};
810
811
812/* Set up the API message handling tables */
813static clib_error_t *
814nsh_plugin_api_hookup (vlib_main_t *vm)
815{
816  nsh_main_t * nm = &nsh_main;
817#define _(N,n)                                                  \
818  vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base),	\
819			  #n,					\
820			  vl_api_##n##_t_handler,		\
821			  vl_noop_handler,			\
822			  vl_api_##n##_t_endian,		\
823			  vl_api_##n##_t_print,			\
824			  sizeof(vl_api_##n##_t), 1);
825  foreach_nsh_plugin_api_msg;
826#undef _
827
828  return 0;
829}
830
831
832
833
834static uword
835nsh_input_map (vlib_main_t * vm,
836               vlib_node_runtime_t * node,
837               vlib_frame_t * from_frame)
838{
839  u32 n_left_from, next_index, *from, *to_next;
840  nsh_main_t * nm = &nsh_main;
841
842  from = vlib_frame_vector_args(from_frame);
843  n_left_from = from_frame->n_vectors;
844
845  next_index = node->cached_next_index;
846
847  while (n_left_from > 0)
848    {
849      u32 n_left_to_next;
850
851      vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
852
853      while (n_left_from >= 4 && n_left_to_next >= 2)
854	{
855	  u32 bi0, bi1;
856	  vlib_buffer_t * b0, *b1;
857	  u32 next0 = NSH_INPUT_NEXT_DROP, next1 = NSH_INPUT_NEXT_DROP;
858	  uword * entry0, *entry1;
859	  nsh_header_t * hdr0 = 0, *hdr1 = 0;
860	  u32 nsp_nsi0, nsp_nsi1;
861	  u32 error0, error1;
862	  nsh_map_t * map0 = 0, *map1 = 0;
863
864	  /* Prefetch next iteration. */
865	  {
866	    vlib_buffer_t * p2, *p3;
867
868	    p2 = vlib_get_buffer(vm, from[2]);
869	    p3 = vlib_get_buffer(vm, from[3]);
870
871	    vlib_prefetch_buffer_header(p2, LOAD);
872	    vlib_prefetch_buffer_header(p3, LOAD);
873
874	    CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
875	    CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
876	  }
877
878	  bi0 = from[0];
879	  bi1 = from[1];
880	  to_next[0] = bi0;
881	  to_next[1] = bi1;
882	  from += 2;
883	  to_next += 2;
884	  n_left_from -= 2;
885	  n_left_to_next -= 2;
886
887	  error0 = 0;
888	  error1 = 0;
889
890	  b0 = vlib_get_buffer(vm, bi0);
891	  hdr0 = vlib_buffer_get_current(b0);
892	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
893	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
894
895	  b1 = vlib_get_buffer(vm, bi1);
896	  hdr1 = vlib_buffer_get_current(b1);
897	  nsp_nsi1 = clib_net_to_host_u32(hdr1->nsp_nsi);
898	  entry1 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi1);
899
900	  if (PREDICT_FALSE(entry0 == 0))
901	    {
902	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
903	      goto trace0;
904	    }
905
906	  if (PREDICT_FALSE(entry1 == 0))
907	    {
908	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
909	      goto trace1;
910	    }
911
912	  /* Entry should point to a mapping ...*/
913	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
914	  map1 = pool_elt_at_index(nm->nsh_mappings, entry1[0]);
915
916	  if (PREDICT_FALSE(map0 == 0))
917	    {
918	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
919	      goto trace0;
920	    }
921
922	  if (PREDICT_FALSE(map1 == 0))
923	    {
924	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
925	      goto trace1;
926	    }
927
928	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
929	  entry1 = hash_get_mem(nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
930
931	  if (PREDICT_FALSE(entry0 == 0))
932	    {
933	      error0 = NSH_INPUT_ERROR_NO_ENTRY;
934	      goto trace0;
935	    }
936	  if (PREDICT_FALSE(entry1 == 0))
937	    {
938	      error1 = NSH_INPUT_ERROR_NO_ENTRY;
939	      goto trace1;
940	    }
941
942	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
943	  hdr1 = pool_elt_at_index(nm->nsh_entries, entry1[0]);
944
945	  /* set up things for next node to transmit ie which node to handle it and where */
946	  next0 = map0->next_node;
947	  next1 = map1->next_node;
948	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
949	  vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
950
951	trace0: b0->error = error0 ? node->errors[error0] : 0;
952
953	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
954	    {
955	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
956	      tr->nsh_header = *hdr0;
957	    }
958
959	trace1: b1->error = error1 ? node->errors[error1] : 0;
960
961	  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
962	    {
963	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
964	      tr->nsh_header = *hdr1;
965	    }
966
967	  vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
968					  n_left_to_next, bi0, bi1, next0, next1);
969
970	}
971
972      while (n_left_from > 0 && n_left_to_next > 0)
973	{
974	  u32 bi0;
975	  vlib_buffer_t * b0;
976	  u32 next0 = NSH_INPUT_NEXT_DROP;
977	  uword * entry0;
978	  nsh_header_t * hdr0 = 0;
979	  u32 nsp_nsi0;
980	  u32 error0;
981	  nsh_map_t * map0 = 0;
982
983	  bi0 = from[0];
984	  to_next[0] = bi0;
985	  from += 1;
986	  to_next += 1;
987	  n_left_from -= 1;
988	  n_left_to_next -= 1;
989	  error0 = 0;
990
991	  b0 = vlib_get_buffer(vm, bi0);
992	  hdr0 = vlib_buffer_get_current(b0);
993	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
994	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
995
996	  if (PREDICT_FALSE(entry0 == 0))
997	    {
998	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
999	      goto trace00;
1000	    }
1001
1002	  /* Entry should point to a mapping ...*/
1003	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
1004
1005	  if (PREDICT_FALSE(map0 == 0))
1006	    {
1007	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
1008	      goto trace00;
1009	    }
1010
1011	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
1012
1013	  if (PREDICT_FALSE(entry0 == 0))
1014	    {
1015	      error0 = NSH_INPUT_ERROR_NO_ENTRY;
1016	      goto trace00;
1017	    }
1018
1019	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
1020
1021	  /* set up things for next node to transmit ie which node to handle it and where */
1022	  next0 = map0->next_node;
1023	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
1024
1025	trace00: b0->error = error0 ? node->errors[error0] : 0;
1026
1027	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1028	    {
1029	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
1030	      tr->nsh_header = *hdr0;
1031	    }
1032
1033	  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
1034					  n_left_to_next, bi0, next0);
1035	}
1036
1037      vlib_put_next_frame(vm, node, next_index, n_left_to_next);
1038
1039    }
1040
1041  return from_frame->n_vectors;
1042}
1043
1044
1045static char * nsh_input_error_strings[] = {
1046#define _(sym,string) string,
1047  foreach_nsh_input_error
1048#undef _
1049};
1050
1051VLIB_REGISTER_NODE (nsh_input_node) = {
1052  .function = nsh_input_map,
1053  .name = "nsh-input",
1054  .vector_size = sizeof (u32),
1055  .format_trace = format_nsh_input_map_trace,
1056  .format_buffer = format_nsh_header_with_length,
1057  .type = VLIB_NODE_TYPE_INTERNAL,
1058
1059  .n_errors = ARRAY_LEN(nsh_input_error_strings),
1060  .error_strings = nsh_input_error_strings,
1061
1062  .n_next_nodes = NSH_INPUT_N_NEXT,
1063
1064  .next_nodes = {
1065#define _(s,n) [NSH_INPUT_NEXT_##s] = n,
1066    foreach_nsh_input_next
1067#undef _
1068  },
1069};
1070
1071clib_error_t *nsh_init (vlib_main_t *vm)
1072{
1073  nsh_main_t *nm = &nsh_main;
1074  clib_error_t * error = 0;
1075  vlib_node_t * vxlan4_gpe_input_node = 0;
1076  vlib_node_t * vxlan6_gpe_input_node = 0;
1077  vlib_node_t * gre_input_node = 0;
1078  u8 * name;
1079
1080  /* Init the main structures from VPP */
1081  nm->vlib_main = vm;
1082  nm->vnet_main = vnet_get_main();
1083
1084  /* Various state maintenance mappings */
1085  nm->nsh_mapping_by_key
1086    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1087
1088  nm->nsh_mapping_by_mapped_key
1089    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1090
1091  nm->nsh_entry_by_key
1092    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1093
1094  name = format (0, "nsh_%08x%c", api_version, 0);
1095
1096  /* Set up the API */
1097  nm->msg_id_base = vl_msg_api_get_msg_ids
1098    ((char *) name, VL_MSG_FIRST_AVAILABLE);
1099
1100  error = nsh_plugin_api_hookup (vm);
1101
1102  /* Add dispositions to nodes that feed nsh-input */
1103  vxlan4_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan4-gpe-input");
1104  ASSERT(vxlan4_gpe_input_node);
1105  //alagalah - validate we don't really need to use the node value
1106  vlib_node_add_next (vm, vxlan4_gpe_input_node->index, nsh_input_node.index);
1107
1108  vxlan6_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan6-gpe-input");
1109  ASSERT(vxlan6_gpe_input_node);
1110  vlib_node_add_next (vm, vxlan6_gpe_input_node->index, nsh_input_node.index);
1111
1112  gre_input_node = vlib_get_node_by_name (vm, (u8 *)"gre-input");
1113  ASSERT(gre_input_node);
1114  vlib_node_add_next (vm, gre_input_node->index, nsh_input_node.index);
1115
1116  vec_free(name);
1117
1118  return error;
1119}
1120
1121VLIB_INIT_FUNCTION(nsh_init);
1122