summaryrefslogtreecommitdiffstats
path: root/src/plugins/unittest/punt_test.c
blob: 4a7503687db7fbcda9f048e47ecded3818113a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; backgro
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/vnet.h>
#include <vnet/adj/rewrite.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/adj/adj.h>
#include <vnet/ip/ip.h>

typedef enum punt_next_t_
{
  PUNT_NEXT_DROP,
  PUNT_N_NEXT,
} punt_next_t;

typedef struct punt_trace_t_
{
  vlib_punt_reason_t pt_reason;
} punt_trace_t;

#define SW_IF_INDEX_PG0 1
#define SW_IF_INDEX_PG1 2

index_t *adjs[FIB_PROTOCOL_IP_MAX];

static vlib_punt_reason_t punt_reason_v4, punt_reason_v6;
static vlib_punt_hdl_t punt_hdl;

static u8 *
format_punt_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  punt_trace_t *t = va_arg (*args, punt_trace_t *);

  s = format (s, "punt: %U", format_vlib_punt_reason, t->pt_reason);

  return s;
}

always_inline uword
punt_test_fwd (vlib_main_t * vm,
	       vlib_node_runtime_t * node,
	       vlib_frame_t * frame, fib_protocol_t fproto, u32 sw_if_index)
{
  u32 n_left_from, *from, *to_next, next_index;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  ip_adjacency_t *adj0;
	  vlib_buffer_t *b0;
	  void *ip0;
	  index_t ai0;
	  u32 bi0;

	  bi0 = to_next[0] = from[0];
	  from += 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  n_left_from -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index;
	  ai0 = adjs[fproto][sw_if_index];

	  adj0 = adj_get (ai0);
	  ip0 = vlib_buffer_get_current (b0);

	  vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
	  vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next, bi0, 0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  return frame->n_vectors;
}

always_inline uword
punt_test_pg0_ip4 (vlib_main_t * vm,
		   vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_fwd (vm, node, frame, FIB_PROTOCOL_IP4, SW_IF_INDEX_PG0));
}

always_inline uword
punt_test_pg1_ip4 (vlib_main_t * vm,
		   vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_fwd (vm, node, frame, FIB_PROTOCOL_IP4, SW_IF_INDEX_PG1));
}

always_inline uword
punt_test_pg0_ip6 (vlib_main_t * vm,
		   vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_fwd (vm, node, frame, FIB_PROTOCOL_IP6, SW_IF_INDEX_PG0));
}

always_inline uword
punt_test_pg1_ip6 (vlib_main_t * vm,
		   vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_fwd (vm, node, frame, FIB_PROTOCOL_IP6, SW_IF_INDEX_PG1));
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (punt_test_pg0_ip4_node) = {
  .function = punt_test_pg0_ip4,
  .name = "punt-test-pg0-ip4",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_trace,
};
VLIB_REGISTER_NODE (punt_test_pg1_ip4_node) = {
  .function = punt_test_pg1_ip4,
  .name = "punt-test-pg1-ip4",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_trace,
};
VLIB_REGISTER_NODE (punt_test_pg0_ip6_node) = {
  .function = punt_test_pg0_ip6,
  .name = "punt-test-pg0-ip6",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_trace,
};
VLIB_REGISTER_NODE (punt_test_pg1_ip6_node) = {
  .function = punt_test_pg1_ip6,
  .name = "punt-test-pg1-ip6",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_trace,
};
/* *INDENT-ON* */

typedef struct punt_feat_trace_t_
{
  vlib_punt_reason_t pt_reason;
} punt_feat_trace_t;

always_inline uword
punt_test_feat_inline (vlib_main_t * vm,
		       vlib_node_runtime_t * node,
		       vlib_frame_t * frame, u8 is_ip4)
{
  u32 n_left_from, *from, *to_next, next_index;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  vlib_buffer_t *b0;
	  u32 bi0, next0;

	  bi0 = to_next[0] = from[0];
	  from += 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  n_left_from -= 1;
	  next0 = 0;

	  b0 = vlib_get_buffer (vm, bi0);

	  if (is_ip4)
	    b0->punt_reason = punt_reason_v4;
	  else
	    b0->punt_reason = punt_reason_v6;

	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
	    {
	      punt_feat_trace_t *t;

	      b0 = vlib_get_buffer (vm, bi0);

	      t = vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->pt_reason = b0->punt_reason;
	    }
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  return frame->n_vectors;
}

static u8 *
format_punt_feat_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  punt_feat_trace_t *t = va_arg (*args, punt_feat_trace_t *);

  s = format (s, "reason: %U", format_vlib_punt_reason, t->pt_reason);

  return s;
}

always_inline uword
punt_test_feat_ip4 (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_feat_inline (vm, node, frame, 1));
}

always_inline uword
punt_test_feat_ip6 (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (punt_test_feat_inline (vm, node, frame, 0));
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (punt_test_feat_ip6_node) = {
  .function = punt_test_feat_ip6,
  .name = "punt-test-feat-ip6",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_feat_trace,
  .n_next_nodes = 1,
  .next_nodes = {
    [0] = "punt-dispatch"
  }
};
VLIB_REGISTER_NODE (punt_test_feat_ip4_node) = {
  .function = punt_test_feat_ip4,
  .name = "punt-test-feat-ip4",
  .vector_size = sizeof (u32),
  .format_trace = format_punt_feat_trace,
  .n_next_nodes = 1,
  .next_nodes = {
    [0] = "punt-dispatch"
  }
};
VNET_FEATURE_INIT (punt_test_feat_ip6_feature, static) =
{
  .arc_name = "ip6-unicast",
  .node_name = "punt-test-feat-ip6",
};
VNET_FEATURE_INIT (punt_test_feat_ip4_feature, static) =
{
  .arc_name = "ip4-unicast",
  .node_name = "punt-test-feat-ip4",
};
/* *INDENT-ON* */

static clib_error_t *
punt_test (vlib_main_t * vm,
	   unformat_input_t * input, vlib_cli_command_t * cmd_arg)
{
  ip46_address_t ip46 = ip46_address_initializer;
  fib_protocol_t fproto;
  vnet_main_t *vnm;
  u32 sw_if_index;
  int rc;

  vnm = vnet_get_main ();
  fproto = FIB_PROTOCOL_IP4;

  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
    {
      vlib_node_t *from;

      if (unformat (input, "%U", unformat_ip4_address, &ip46.ip4))
	{
	  fproto = FIB_PROTOCOL_IP4;
	}
      else if (unformat (input, "%U", unformat_ip6_address, &ip46.ip6))
	{
	  fproto = FIB_PROTOCOL_IP6;
	}
      else if (unformat (input, "clear"))
	{
	  vnet_feature_enable_disable ("ip4-unicast",
				       "punt-test-feat-ip4",
				       sw_if_index, 0, NULL, 0);
	  vnet_feature_enable_disable ("ip6-unicast",
				       "punt-test-feat-ip6",
				       sw_if_index, 0, NULL, 0);
	  return NULL;
	}
      else
	{
	  /*
	   * allocate a client and a reason
	   */
	  punt_hdl = vlib_punt_client_register ("test");

	  rc =
	    vlib_punt_reason_alloc (punt_hdl, "reason-v4", &punt_reason_v4);
	  rc |=
	    vlib_punt_reason_alloc (punt_hdl, "reason-v6", &punt_reason_v6);
	  ASSERT (!rc);

	  vnet_feature_enable_disable ("ip4-unicast",
				       "punt-test-feat-ip4",
				       sw_if_index, 1, NULL, 0);
	  vnet_feature_enable_disable ("ip6-unicast",
				       "punt-test-feat-ip6",
				       sw_if_index, 1, NULL, 0);
	  return NULL;
	}

      if (SW_IF_INDEX_PG0 == sw_if_index)
	{
	  if (FIB_PROTOCOL_IP4 == fproto)
	    {
	      /*
	       * register the node that will forward the punted packet
	       */
	      vlib_punt_register (punt_hdl, punt_reason_v4,
				  "punt-test-pg0-ip4");
	      from = vlib_get_node_by_name (vm, (u8 *) "punt-test-pg0-ip4");
	    }
	  else
	    {
	      vlib_punt_register (punt_hdl, punt_reason_v6,
				  "punt-test-pg0-ip6");
	      from = vlib_get_node_by_name (vm, (u8 *) "punt-test-pg0-ip6");
	    }
	}
      else
	{
	  if (FIB_PROTOCOL_IP4 == fproto)
	    {
	      vlib_punt_register (punt_hdl, punt_reason_v4,
				  "punt-test-pg1-ip4");
	      from = vlib_get_node_by_name (vm, (u8 *) "punt-test-pg1-ip4");
	    }
	  else
	    {
	      vlib_punt_register (punt_hdl, punt_reason_v6,
				  "punt-test-pg1-ip6");
	      from = vlib_get_node_by_name (vm, (u8 *) "punt-test-pg1-ip6");
	    }
	}

      vlib_node_add_next (vm, from->index,
			  vnet_tx_node_index_for_sw_interface
			  (vnm, sw_if_index));

      vec_validate (adjs[fproto], sw_if_index);

      adjs[fproto][sw_if_index] = adj_nbr_find (fproto,
						fib_proto_to_link (fproto),
						&ip46, sw_if_index);
    }

  return (NULL);
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (test_fib_command, static) =
{
  .path = "test punt",
  .short_help = "punt unit tests - DO NOT RUN ON A LIVE SYSTEM",
  .function = punt_test,
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */