Plugin for IP-Address to Interface Punting
[vpp.git] / src / plugins / stn / stn.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stn/stn.h>
17
18 #include <vnet/plugin/plugin.h>
19 #include <vpp/app/version.h>
20 #include <vnet/ip/format.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/udp/udp.h>
23 #include <vnet/tcp/tcp.h>
24
25 stn_main_t stn_main;
26 static vlib_node_registration_t stn_ip4_punt;
27 static vlib_node_registration_t stn_ip6_punt;
28
29 static u8 stn_hw_addr_local[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
30 static u8 stn_hw_addr_dst[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
31
32 static ethernet_header_t stn_ip4_ethernet_header = {};
33 static ethernet_header_t stn_ip6_ethernet_header = {};
34
35 typedef struct {
36   clib_bihash_kv_16_8_t kv;
37 } stn_ip46_punt_trace_t;
38
39 static u8 *
40 format_stn_rule (u8 * s, va_list * args)
41 {
42   stn_rule_t *r = va_arg (*args, stn_rule_t *);
43   stn_main_t *stn = &stn_main;
44   u32 indent = format_get_indent (s);
45   u32 node_index = ip46_address_is_ip4(&r->address)?stn_ip4_punt.index:stn_ip6_punt.index;
46   vlib_node_t *next_node = vlib_get_next_node(vlib_get_main(), node_index, r->next_node_index);
47   s = format (s, "rule_index: %d\n", r - stn->rules);
48   s = format (s, "%Uaddress: %U\n", format_white_space, indent,
49               format_ip46_address, &r->address, IP46_TYPE_ANY);
50   s = format (s, "%Uiface: %U (%d)\n", format_white_space, indent,
51               format_vnet_sw_if_index_name, vnet_get_main(), r->sw_if_index,
52               r->sw_if_index);
53   s = format (s, "%Unext_node: %s (%d)", format_white_space, indent,
54               next_node->name, next_node->index);
55   return s;
56 }
57
58 static_always_inline u8 *
59 format_stn_ip46_punt_trace (u8 * s, va_list * args, u8 is_ipv4)
60 {
61   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63   stn_ip46_punt_trace_t *t = va_arg (*args, stn_ip46_punt_trace_t *);
64   u32 indent = format_get_indent (s);
65
66   format (s, "dst_address: %U\n", format_ip46_address,
67           (ip46_address_t *)&t->kv.key, IP46_TYPE_ANY);
68
69   if (t->kv.value == ~(0L))
70     {
71       s = format (s, "%Urule: none", format_white_space, indent);
72     }
73   else
74     {
75       s = format (s, "%Urule:\n%U%U", format_white_space, indent,
76                      format_white_space, indent + 2,
77                      format_stn_rule, &stn_main.rules[t->kv.value]);
78     }
79   return s;
80 }
81
82 static void
83 stn_punt_fn (vlib_main_t * vm,
84                    vlib_node_runtime_t * node, vlib_frame_t * frame)
85 {
86   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
87   stn_main_t *stn = &stn_main;
88
89   from = vlib_frame_vector_args (frame);
90   n_left_from = frame->n_vectors;
91   next_index = node->cached_next_index;
92
93   while (n_left_from > 0)
94     {
95       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
96
97       /* Single loop */
98       while (n_left_from > 0 && n_left_to_next > 0)
99         {
100           u32 pi0;
101           vlib_buffer_t *p0;
102           u32 next0;
103
104           pi0 = to_next[0] = from[0];
105           from += 1;
106           n_left_from -= 1;
107           to_next += 1;
108           n_left_to_next -= 1;
109
110           p0 = vlib_get_buffer (vm, pi0);
111
112           ip4_header_t *ip = vlib_buffer_get_current(p0);
113           if ((ip->ip_version_and_header_length & 0xf0) == 0x40)
114             next0 = stn->punt_to_stn_ip4_next_index;
115           else
116             next0 = stn->punt_to_stn_ip6_next_index;
117
118           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
119                                            n_left_to_next, pi0, next0);
120         }
121       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
122     }
123 }
124
125 typedef enum
126 {
127   STN_IP_PUNT_DROP,
128   STN_IP_PUNT_N_NEXT,
129 } stn_ip_punt_next_t;
130
131 static_always_inline uword
132 stn_ip46_punt_fn (vlib_main_t * vm,
133                    vlib_node_runtime_t * node, vlib_frame_t * frame,
134                    u8 is_ipv4)
135 {
136   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
137   stn_main_t *stn = &stn_main;
138
139   from = vlib_frame_vector_args (frame);
140   n_left_from = frame->n_vectors;
141   next_index = node->cached_next_index;
142
143   while (n_left_from > 0)
144     {
145       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
146
147       /* Single loop */
148       while (n_left_from > 0 && n_left_to_next > 0)
149         {
150           u32 pi0;
151           vlib_buffer_t *p0;
152           clib_bihash_kv_16_8_t kv;
153           u32 next0 = STN_IP_PUNT_DROP;
154
155           pi0 = to_next[0] = from[0];
156           from += 1;
157           n_left_from -= 1;
158           to_next += 1;
159           n_left_to_next -= 1;
160
161           p0 = vlib_get_buffer (vm, pi0);
162
163           if (is_ipv4)
164             {
165               ip4_header_t *hdr = (ip4_header_t *) vlib_buffer_get_current(p0);
166               ip46_address_set_ip4((ip46_address_t *)kv.key, &hdr->dst_address);
167             }
168           else
169             {
170               ip6_header_t *hdr = (ip6_header_t *) vlib_buffer_get_current(p0);
171               kv.key[0] = hdr->dst_address.as_u64[0];
172               kv.key[1] = hdr->dst_address.as_u64[1];
173             }
174
175           kv.value = ~(0L);
176           clib_bihash_search_inline_16_8 (&stn->rule_by_address_table, &kv);
177           if (kv.value != ~(0L))
178             {
179               ethernet_header_t *eth;
180               stn_rule_t *r = &stn->rules[kv.value];
181               vnet_buffer(p0)->sw_if_index[VLIB_TX] = r->sw_if_index;
182               next0 = r->next_node_index;
183               vlib_buffer_advance(p0, -sizeof(*eth));
184               eth = (ethernet_header_t *) vlib_buffer_get_current(p0);
185               if (is_ipv4)
186                 clib_memcpy(eth, &stn_ip4_ethernet_header, sizeof(*eth));
187               else
188                 clib_memcpy(eth, &stn_ip6_ethernet_header, sizeof(*eth));
189             }
190
191           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
192             {
193               stn_ip46_punt_trace_t *tr =
194                   vlib_add_trace (vm, node, p0, sizeof (*tr));
195               tr->kv = kv;
196             }
197
198           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
199                                            n_left_to_next, pi0, next0);
200         }
201       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
202     }
203
204   return frame->n_vectors;
205 }
206
207
208 #define foreach_stn_ip_punt_error \
209  _(NONE, "no error")
210
211 typedef enum {
212 #define _(sym,str) STN_IP_punt_ERROR_##sym,
213   foreach_stn_ip_punt_error
214 #undef _
215   STN_IP_PUNT_N_ERROR,
216 } ila_error_t;
217
218 static char *stn_ip_punt_error_strings[] = {
219 #define _(sym,string) string,
220     foreach_stn_ip_punt_error
221 #undef _
222 };
223
224 u8 *
225 format_stn_ip6_punt_trace (u8 * s, va_list * args)
226 {
227   return format_stn_ip46_punt_trace (s, args, 0);
228 }
229
230 static uword
231 stn_ip6_punt_fn (vlib_main_t * vm,
232                    vlib_node_runtime_t * node, vlib_frame_t * frame)
233 {
234   return stn_ip46_punt_fn(vm, node, frame, 0);
235 }
236
237 /** *INDENT-OFF* */
238 VLIB_REGISTER_NODE (stn_ip6_punt, static) =
239 {
240   .function = stn_ip6_punt_fn,
241   .name = "stn-ip6-punt",
242   .vector_size = sizeof (u32),
243   .format_trace = format_stn_ip6_punt_trace,
244   .n_errors = STN_IP_PUNT_N_ERROR,
245   .error_strings = stn_ip_punt_error_strings,
246   .n_next_nodes = STN_IP_PUNT_N_NEXT,
247   .next_nodes =
248   {
249       [STN_IP_PUNT_DROP] = "error-drop"
250   },
251 };
252 /** *INDENT-ON* */
253
254 u8 *
255 format_stn_ip4_punt_trace (u8 * s, va_list * args)
256 {
257   return format_stn_ip46_punt_trace (s, args, 1);
258 }
259
260 static uword
261 stn_ip4_punt_fn (vlib_main_t * vm,
262                    vlib_node_runtime_t * node, vlib_frame_t * frame)
263 {
264   return stn_ip46_punt_fn(vm, node, frame, 1);
265 }
266
267 /** *INDENT-OFF* */
268 VLIB_REGISTER_NODE (stn_ip4_punt, static) =
269 {
270   .function = stn_ip4_punt_fn,
271   .name = "stn-ip4-punt",
272   .vector_size = sizeof (u32),
273   .format_trace = format_stn_ip4_punt_trace,
274   .n_errors = STN_IP_PUNT_N_ERROR,
275   .error_strings = stn_ip_punt_error_strings,
276   .n_next_nodes = STN_IP_PUNT_N_NEXT,
277   .next_nodes =
278   {
279       [STN_IP_PUNT_DROP] = "error-drop",
280   },
281 };
282 /** *INDENT-ON* */
283
284 clib_error_t *
285 stn_init (vlib_main_t * vm)
286 {
287   stn_main_t *stn = &stn_main;
288   stn->rules = 0;
289   clib_bihash_init_16_8(&stn->rule_by_address_table, "stn addresses",
290                         1024, 1<<20);
291
292   clib_memcpy(stn_ip4_ethernet_header.dst_address, stn_hw_addr_dst, 6);
293   clib_memcpy(stn_ip4_ethernet_header.src_address, stn_hw_addr_local, 6);
294   stn_ip4_ethernet_header.type = clib_host_to_net_u16(ETHERNET_TYPE_IP4);
295
296   clib_memcpy(stn_ip6_ethernet_header.dst_address, stn_hw_addr_dst, 6);
297   clib_memcpy(stn_ip6_ethernet_header.src_address, stn_hw_addr_local, 6);
298   stn_ip6_ethernet_header.type = clib_host_to_net_u16(ETHERNET_TYPE_IP6);
299
300   u32 punt_node_index = vlib_get_node_by_name(vm, (u8 *)"error-punt")->index;
301   stn->punt_to_stn_ip4_next_index =
302       vlib_node_add_next(vm, punt_node_index, stn_ip4_punt.index);
303   stn->punt_to_stn_ip6_next_index =
304         vlib_node_add_next(vm, punt_node_index, stn_ip6_punt.index);
305
306   return stn_api_init (vm, stn);
307
308   return NULL;
309 }
310
311 VLIB_INIT_FUNCTION (stn_init);
312
313 /* *INDENT-OFF* */
314 VLIB_PLUGIN_REGISTER () = {
315     .version = VPP_BUILD_VER,
316     .description = "VPP Steals the NIC for Container integration",
317 };
318 /* *INDENT-ON* */
319
320 int stn_rule_add_del (stn_rule_add_del_args_t *args)
321 {
322   vnet_main_t *vnm = vnet_get_main();
323   vlib_main_t *vm = vlib_get_main();
324   stn_main_t *stn = &stn_main;
325
326   stn_rule_t *r = NULL;
327   clib_bihash_kv_16_8_t kv;
328   kv.key[0] = args->address.as_u64[0];
329   kv.key[1] = args->address.as_u64[1];
330
331   if (clib_bihash_search_inline_16_8 (&stn->rule_by_address_table, &kv) == 0)
332     {
333       r = &stn->rules[kv.value];
334     }
335   else if (!args->del)
336     {
337       pool_get(stn->rules, r);
338       kv.value = r - stn->rules;
339       clib_bihash_add_del_16_8(&stn->rule_by_address_table, &kv, 1);
340       r->address = args->address;
341
342       stn->n_rules++;
343       if (stn->n_rules == 1)
344         {
345           foreach_vlib_main({
346             this_vlib_main->os_punt_frame = stn_punt_fn;
347           });
348           udp_punt_unknown(vm, 0, 1);
349           udp_punt_unknown(vm, 1, 1);
350           tcp_punt_unknown(vm, 0, 1);
351           tcp_punt_unknown(vm, 1, 1);
352         }
353     }
354
355   if (!args->del)
356     {
357       /* Getting output node and adding it as next */
358       u32 output_node_index =
359           vnet_tx_node_index_for_sw_interface(vnm, args->sw_if_index);
360       u32 node_index = ip46_address_is_ip4(&args->address)?
361           stn_ip4_punt.index : stn_ip6_punt.index;
362
363       r->sw_if_index = args->sw_if_index;
364       r->next_node_index =
365           vlib_node_add_next(vm, node_index, output_node_index);
366
367       /* enabling forwarding on the output node (might not be done since
368        * it is unnumbered) */
369       vnet_feature_enable_disable("ip4-unicast", "ip4-lookup", args->sw_if_index,
370                                   1, 0, 0);
371       vnet_feature_enable_disable("ip6-unicast", "ip6-lookup", args->sw_if_index,
372                                   1, 0, 0);
373       vnet_feature_enable_disable("ip4-unicast", "ip4-drop", args->sw_if_index,
374                                   0, 0, 0);
375       vnet_feature_enable_disable("ip6-unicast", "ip6-drop", args->sw_if_index,
376                                   0, 0, 0);
377     }
378   else if (r)
379     {
380       clib_bihash_add_del_16_8(&stn->rule_by_address_table, &kv, 0);
381       pool_put(stn->rules, r);
382
383       stn->n_rules--;
384       if (stn->n_rules == 0)
385         {
386           foreach_vlib_main({
387             this_vlib_main->os_punt_frame = NULL;
388           });
389         }
390     }
391   else
392     {
393       return VNET_API_ERROR_NO_SUCH_ENTRY;
394     }
395
396   return 0;
397 }
398
399 static clib_error_t *
400 show_stn_rules_fn (vlib_main_t * vm,
401                       unformat_input_t * input, vlib_cli_command_t * cmd)
402 {
403   stn_main_t *stn = &stn_main;
404   u8 *s = 0;
405   stn_rule_t *rule;
406   pool_foreach(rule, stn->rules, {
407       s = format (s, "- %U\n", format_stn_rule, rule);
408   });
409
410   vlib_cli_output(vm, "%v", s);
411
412   vec_free(s);
413   return NULL;
414 }
415
416 VLIB_CLI_COMMAND (show_stn_rules_command, static) =
417 {
418   .path = "show stn rules",
419   .short_help = "",
420   .function = show_stn_rules_fn,
421 };
422
423 static clib_error_t *
424 stn_rule_fn (vlib_main_t * vm,
425                       unformat_input_t * input, vlib_cli_command_t * cmd)
426 {
427   unformat_input_t _line_input, *line_input = &_line_input;
428   clib_error_t *error = 0;
429   stn_rule_add_del_args_t args = {};
430   u8 got_addr = 0;
431   u8 got_iface = 0;
432   int ret;
433
434   if (!unformat_user (input, unformat_line_input, line_input))
435     return 0;
436
437   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
438     {
439       if (unformat (line_input, "address %U", unformat_ip46_address,
440                     &args.address, IP46_TYPE_ANY))
441         got_addr = 1;
442       else if (unformat
443                (line_input, "interface %U", unformat_vnet_sw_interface,
444                 vnet_get_main(), &args.sw_if_index))
445         got_iface = 1;
446       else if (unformat (line_input, "del"))
447         args.del = 1;
448       else
449         {
450           error = clib_error_return (0, "parse error: '%U'",
451                                      format_unformat_error, line_input);
452           goto done;
453         }
454     }
455
456   if (!got_addr)
457     {
458       error = clib_error_return (0, "Missing address");
459       goto done;
460     }
461
462   if (!got_iface)
463     {
464       error = clib_error_return (0, "Missing interface");
465       goto done;
466     }
467
468   if ((ret = stn_rule_add_del (&args)))
469     {
470       error = clib_error_return (0, "stn_rule_add_del returned error %d", ret);
471       goto done;
472     }
473
474 done:
475   unformat_free (line_input);
476   return error;
477 }
478
479 VLIB_CLI_COMMAND (stn_rule_command, static) =
480 {
481   .path = "stn rule",
482   .short_help = "address <addr> interface <iface> [del]",
483   .function = stn_rule_fn,
484 };