aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/feature/registration.c
blob: fb10fbbe0db7b6e928b5804d388b9fcb438b7105 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright (c) 2016 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/ip/ip.h>
#include <vnet/mpls/mpls.h>

/**
 * @file
 * @brief Feature Subgraph Ordering.

    Dynamically compute feature subgraph ordering by performing a
    topological sort across a set of "feature A before feature B" and
    "feature C after feature B" constraints.

    Use the topological sort result to set up vnet_config_main_t's for
    use at runtime.

    Feature subgraph arcs are simple enough. They start at specific
    fixed nodes, and end at specific fixed nodes.  In between, a
    per-interface current feature configuration dictates which
    additional nodes each packet visits. Each so-called feature node
    can [of course] drop any specific packet.

    See ip4_forward.c, ip6_forward.c in this directory to see the
    current rx-unicast, rx-multicast, and tx feature subgraph arc
    definitions.

    Let's say that we wish to add a new feature to the ip4 unicast
    feature subgraph arc, which needs to run before @c ip4-lookup.  In
    either base code or a plugin,
    <CODE><PRE>
    \#include <vnet/feature/feature.h>
    </PRE></CODE>

    and add the new feature as shown:

    <CODE><PRE>
    VNET_FEATURE_INIT (ip4_lookup, static) =
    {
      .arch_name = "ip4-unicast",
      .node_name = "my-ip4-unicast-feature",
      .runs_before = VLIB_FEATURES ("ip4-lookup")
    };
    </PRE></CODE>

    Here's the standard coding pattern to enable / disable
    @c my-ip4-unicast-feature on an interface:

    <CODE><PRE>

    sw_if_index = <interface-handle>
    vnet_feature_enable_disable ("ip4-unicast", "my-ip4-unicast-feature",
                                 sw_if_index, 1 );
    </PRE></CODE>

    Here's how to obtain the correct next node index in packet
    processing code, aka in the implementation of @c my-ip4-unicast-feature:

    <CODE><PRE>
    vnet_feature_next (sw_if_index0, &next0, b0);

    </PRE></CODE>

    Nodes are free to drop or otherwise redirect packets. Packets
    which "pass" should be enqueued via the next0 arc computed by
    vnet_feature_next.
*/


static int
comma_split (u8 * s, u8 ** a, u8 ** b)
{
  *a = s;

  while (*s && *s != ',')
    s++;

  if (*s == ',')
    *s = 0;
  else
    return 1;

  *b = (u8 *) (s + 1);
  return 0;
}

/**
 * @brief Initialize a feature graph arc
 * @param vm vlib main structure pointer
 * @param vcm vnet config main structure pointer
 * @param feature_start_nodes names of start-nodes which use this
 *	  feature graph arc
 * @param num_feature_start_nodes number of start-nodes
 * @param first_reg first element in
 *        [an __attribute__((constructor)) function built, or
 *        otherwise created] singly-linked list of feature registrations
 * @param first_const first element in
 *        [an __attribute__((constructor)) function built, or
 *        otherwise created] singly-linked list of bulk order constraints
 * @param [out] in_feature_nodes returned vector of
 *        topologically-sorted feature node names, for use in
 *        show commands
 * @returns 0 on success, otherwise an error message. Errors
 *        are fatal since they invariably involve mistyped node-names, or
 *        genuinely missing node-names
 */
clib_error_t *
vnet_feature_arc_init (vlib_main_t * vm,
		       vnet_config_main_t * vcm,
		       char **feature_start_nodes,
		       int num_feature_start_nodes,
		       vnet_feature_registration_t * first_reg,
		       vnet_feature_constraint_registration_t *
		       first_const_set, char ***in_feature_nodes)
{
  uword *index_by_name;
  uword *reg_by_index;
  u8 **node_names = 0;
  u8 *node_name;
  char *prev_name;
  char **these_constraints;
  char *this_constraint_c;
  u8 **constraints = 0;
  u8 *constraint_tuple;
  u8 *this_constraint;
  u8 **orig, **closure;
  uword *p;
  int i, j, k;
  u8 *a_name, *b_name;
  int a_index, b_index;
  int n_features;
  u32 *result = 0;
  vnet_feature_registration_t *this_reg = 0;
  vnet_feature_constraint_registration_t *this_const_set = 0;
  char **feature_nodes = 0;
  hash_pair_t *hp;
  u8 **keys_to_delete = 0;

  index_by_name = hash_create_string (0, sizeof (uword));
  reg_by_index = hash_create (0, sizeof (uword));

  this_reg = first_reg;

  /* pass 1, collect feature node names, construct a before b pairs */
  while (this_reg)
    {
      node_name = format (0, "%s%c", this_reg->node_name, 0);
      hash_set (reg_by_index, vec_len (node_names), (uword) this_reg);

      hash_set_mem (index_by_name, node_name, vec_len (node_names));

      vec_add1 (node_names, node_name);

      these_constraints = this_reg->runs_before;
      while (these_constraints && these_constraints[0])
	{
	  this_constraint_c = these_constraints[0];

	  constraint_tuple = format (0, "%s,%s%c", node_name,
				     this_constraint_c, 0);
	  vec_add1 (constraints, constraint_tuple);
	  these_constraints++;
	}

      these_constraints = this_reg->runs_after;
      while (these_constraints && these_constraints[0])
	{
	  this_constraint_c = these_constraints[0];

	  constraint_tuple = format (0, "%s,%s%c",
				     this_constraint_c, node_name, 0);
	  vec_add1 (constraints, constraint_tuple);
	  these_constraints++;
	}

      this_reg = this_reg->next_in_arc;
    }

  /* pass 2, collect bulk "a then b then c then d" constraints */
  this_const_set = first_const_set;
  while (this_const_set)
    {
      these_constraints = this_const_set->node_names;

      prev_name = 0;
      /* Across the list of constraints */
      while (these_constraints && these_constraints[0])
	{
	  this_constraint_c = these_constraints[0];
	  p = hash_get_mem (index_by_name, this_constraint_c);
	  if (p == 0)
	    {
	      clib_warning
		("bulk constraint feature node '%s' not found for arc '%s'",
		 this_constraint_c);
	      these_constraints++;
	      continue;
	    }

	  if (prev_name == 0)
	    {
	      prev_name = this_constraint_c;
	      these_constraints++;
	      continue;
	    }

	  constraint_tuple = format (0, "%s,%s%c", prev_name,
				     this_constraint_c, 0);
	  vec_add1 (constraints, constraint_tuple);
	  prev_name = this_constraint_c;
	  these_constraints++;
	}

      this_const_set = this_const_set->next_in_arc;
    }

  n_features = vec_len (node_names);
  orig = clib_ptclosure_alloc (n_features);

  for (i = 0; i < vec_len (constraints); i++)
    {
      this_constraint = constraints[i];

      if (comma_split (this_constraint, &a_name, &b_name))
	return clib_error_return (0, "comma_split failed!");

      p = hash_get_mem (index_by_name, a_name);
      /*
       * Note: the next two errors mean that something is
       * b0rked. As in: if you code "A depends on B," and you forget
       * to define a FEATURE_INIT macro for B, you lose.
       * Nonexistent graph nodes are tolerated.
       */
      if (p == 0)
	{
	  clib_warning ("feature node '%s' not found (before '%s', arc '%s')",
			a_name, b_name, first_reg->arc_name);
	  continue;
	}
      a_index = p[0];

      p = hash_get_mem (index_by_name, b_name);
      if (p == 0)
	{
	  clib_warning ("feature node '%s' not found (after '%s', arc '%s')",
			b_name, a_name, first_reg->arc_name);
	  continue;
	}
      b_index = p[0];

      /* add a before b to the original set of constraints */
      orig[a_index][b_index] = 1;
      vec_free (this_constraint);
    }

  /* Compute the positive transitive closure of the original constraints */
  closure = clib_ptclosure (orig);

  /* Compute a partial order across feature nodes, if one exists. */
again:
  for (i = 0; i < n_features; i++)
    {
      for (j = 0; j < n_features; j++)
	{
	  if (closure[i][j])
	    goto item_constrained;
	}
      /* Item i can be output */
      vec_add1 (result, i);
      {
	for (k = 0; k < n_features; k++)
	  closure[k][i] = 0;
	/*
	 * Add a "Magic" a before a constraint.
	 * This means we'll never output it again
	 */
	closure[i][i] = 1;
	goto again;
      }
    item_constrained:
      ;
    }

  /* see if we got a partial order... */
  if (vec_len (result) != n_features)
    return clib_error_return
      (0, "Arc '%s': failed to find a suitable feature order!",
       first_reg->arc_name);

  /*
   * We win.
   * Bind the index variables, and output the feature node name vector
   * using the partial order we just computed. Result is in stack
   * order, because the entry with the fewest constraints (e.g. none)
   * is output first, etc.
   */

  for (i = n_features - 1; i >= 0; i--)
    {
      p = hash_get (reg_by_index, result[i]);
      ASSERT (p != 0);
      this_reg = (vnet_feature_registration_t *) p[0];
      if (this_reg->feature_index_ptr)
	*this_reg->feature_index_ptr = n_features - (i + 1);
      this_reg->feature_index = n_features - (i + 1);
      vec_add1 (feature_nodes, this_reg->node_name);
    }

  /* Set up the config infrastructure */
  vnet_config_init (vm, vcm,
		    feature_start_nodes,
		    num_feature_start_nodes,
		    feature_nodes, vec_len (feature_nodes));

  /* Save a copy for show command */
  *in_feature_nodes = feature_nodes;

  /* Finally, clean up all the shit we allocated */
  /* *INDENT-OFF* */
  hash_foreach_pair (hp, index_by_name,
  ({
    vec_add1 (keys_to_delete, (u8 *)hp->key);
  }));
  /* *INDENT-ON* */
  hash_free (index_by_name);
  for (i = 0; i < vec_len (keys_to_delete); i++)
    vec_free (keys_to_delete[i]);
  vec_free (keys_to_delete);
  hash_free (reg_by_index);
  vec_free (result);
  clib_ptclosure_free (orig);
  clib_ptclosure_free (closure);
  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
lass="mi">7)); byte = i / 8; bit = 1 << bitnum; a->as_u8[byte] |= bit; } } always_inline uword ip6_address_is_zero (ip6_address_t * a) { int i; for (i = 0; i < ARRAY_LEN (a->as_uword); i++) if (a->as_uword[i] != 0) return 0; return 1; } /* Check for unspecified address ::0 */ always_inline uword ip6_address_is_unspecified (ip6_address_t * a) { return ip6_address_is_zero (a); } /* Check for loopback address ::1 */ always_inline uword ip6_address_is_loopback (ip6_address_t * a) { uword is_loopback; u8 save = a->as_u8[15]; a->as_u8[15] = save ^ 1; is_loopback = ip6_address_is_zero (a); a->as_u8[15] = save; return is_loopback; } /* Check for link local unicast fe80::/10. */ always_inline uword ip6_address_is_link_local_unicast (ip6_address_t * a) { return a->as_u8[0] == 0xfe && (a->as_u8[1] & 0xc0) == 0x80; } /* Check for unique local unicast fc00::/7. */ always_inline uword ip6_address_is_local_unicast (ip6_address_t * a) { return (a->as_u8[0] & 0xfe) == 0xfc; } /* Check for unique global unicast 2000::/3. */ always_inline uword ip6_address_is_global_unicast (ip6_address_t * a) { return (a->as_u8[0] & 0xe0) == 0x20; } /* Check for solicited node multicast 0xff02::1:ff00:0/104 */ always_inline uword ip6_is_solicited_node_multicast_address (ip6_address_t * a) { return (a->as_u32[0] == clib_host_to_net_u32 (0xff020000) && a->as_u32[1] == 0 && a->as_u32[2] == clib_host_to_net_u32 (1) && a->as_u8[12] == 0xff); } typedef struct { /* 4 bit version, 8 bit traffic class and 20 bit flow label. */ u32 ip_version_traffic_class_and_flow_label; /* Total packet length not including this header (but including any extension headers if present). */ u16 payload_length; /* Protocol for next header. */ u8 protocol; /* Hop limit decremented by router at each hop. */ u8 hop_limit; /* Source and destination address. */ ip6_address_t src_address, dst_address; } ip6_header_t; always_inline u8 ip6_traffic_class (ip6_header_t * i) { return (i->ip_version_traffic_class_and_flow_label & 0x0FF00000) >> 20; } always_inline void * ip6_next_header (ip6_header_t * i) { return (void *) (i + 1); } always_inline void ip6_copy_header (ip6_header_t * dst, const ip6_header_t * src) { dst->ip_version_traffic_class_and_flow_label = src->ip_version_traffic_class_and_flow_label; dst->payload_length = src->payload_length; dst->protocol = src->protocol; dst->hop_limit = src->hop_limit; dst->src_address.as_uword[0] = src->src_address.as_uword[0]; dst->src_address.as_uword[1] = src->src_address.as_uword[1]; dst->dst_address.as_uword[0] = src->dst_address.as_uword[0]; dst->dst_address.as_uword[1] = src->dst_address.as_uword[1]; } always_inline void ip6_tcp_reply_x1 (ip6_header_t * ip0, tcp_header_t * tcp0) { { ip6_address_t src0, dst0; src0 = ip0->src_address; dst0 = ip0->dst_address; ip0->src_address = dst0; ip0->dst_address = src0; } { u16 src0, dst0; src0 = tcp0->src; dst0 = tcp0->dst; tcp0->src = dst0; tcp0->dst = src0; } } always_inline void ip6_tcp_reply_x2 (ip6_header_t * ip0, ip6_header_t * ip1, tcp_header_t * tcp0, tcp_header_t * tcp1) { { ip6_address_t src0, dst0, src1, dst1; src0 = ip0->src_address; src1 = ip1->src_address; dst0 = ip0->dst_address; dst1 = ip1->dst_address; ip0->src_address = dst0; ip1->src_address = dst1; ip0->dst_address = src0; ip1->dst_address = src1; } { u16 src0, dst0, src1, dst1; src0 = tcp0->src; src1 = tcp1->src; dst0 = tcp0->dst; dst1 = tcp1->dst; tcp0->src = dst0; tcp1->src = dst1; tcp0->dst = src0; tcp1->dst = src1; } } /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { u8 data; }) ip6_pad1_option_t; /* *INDENT-ON* */ /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { u8 type; u8 len; u8 data[0]; }) ip6_padN_option_t; /* *INDENT-ON* */ /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { #define IP6_MLDP_ALERT_TYPE 0x5 u8 type; u8 len; u16 value; }) ip6_router_alert_option_t; /* *INDENT-ON* */ /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { u8 next_hdr; /* Length of this header plus option data in 8 byte units. */ u8 n_data_u64s; }) ip6_ext_header_t; always_inline u8 ip6_ext_hdr(u8 nexthdr) { /* * find out if nexthdr is an extension header or a protocol */ return (nexthdr == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) || (nexthdr == IP_PROTOCOL_IPV6_FRAGMENTATION) || (nexthdr == IP_PROTOCOL_IPSEC_AH) || (nexthdr == IP_PROTOCOL_IPV6_ROUTE) || (nexthdr == IP_PROTOCOL_IP6_DESTINATION_OPTIONS); } #define ip6_ext_header_len(p) ((((ip6_ext_header_t *)(p))->n_data_u64s+1) << 3) #define ip6_ext_authhdr_len(p) ((((ip6_ext_header_t *)(p))->n_data_u64s+2) << 2) always_inline void * ip6_ext_next_header (ip6_ext_header_t *ext_hdr ) { return (void *)((u8 *) ext_hdr + ip6_ext_header_len(ext_hdr)); } /* * Macro to find the IPv6 ext header of type t * I is the IPv6 header * P is the previous IPv6 ext header (NULL if none) * M is the matched IPv6 ext header of type t */ #define ip6_ext_header_find_t(i, p, m, t) \ if ((i)->protocol == t) \ { \ (m) = (void *)((i)+1); \ (p) = NULL; \ } \ else \ { \ (m) = NULL; \ (p) = (void *)((i)+1); \ while (ip6_ext_hdr((p)->next_hdr) && \ ((ip6_ext_header_t *)(p))->next_hdr != (t)) \ { \ (p) = ip6_ext_next_header((p)); \ } \ if ( ((p)->next_hdr) == (t)) \ { \ (m) = (void *)(ip6_ext_next_header((p))); \ } \ } typedef CLIB_PACKED (struct { u8 next_hdr; /* Length of this header plus option data in 8 byte units. */ u8 n_data_u64s; u8 data[0]; }) ip6_hop_by_hop_ext_t; /* *INDENT-ON* */ /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { u8 next_hdr; u8 rsv; u16 fragment_offset_and_more; u32 identification; }) ip6_frag_hdr_t; /* *INDENT-ON* */ #define ip6_frag_hdr_offset(hdr) \ (clib_net_to_host_u16((hdr)->fragment_offset_and_more) >> 3) #define ip6_frag_hdr_more(hdr) \ (clib_net_to_host_u16((hdr)->fragment_offset_and_more) & 0x1) #define ip6_frag_hdr_offset_and_more(offset, more) \ clib_host_to_net_u16(((offset) << 3) + !!(more)) #endif /* included_ip6_packet_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */