Line data Source code
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 <vnet/vnet.h>
17 : #include <vlib/vlib.h>
18 : #include <vlibapi/api.h>
19 : #include <vlibmemory/api.h>
20 :
21 : #include <vnet/ip/ip.h>
22 : #include <vnet/ip/ip6_hop_by_hop.h>
23 : #include <ioam/encap/ip6_ioam_trace.h>
24 : #include <ioam/encap/ip6_ioam_e2e.h>
25 : #include <ioam/udp-ping/udp_ping_packet.h>
26 : #include <ioam/udp-ping/udp_ping.h>
27 : #include <ioam/udp-ping/udp_ping_util.h>
28 : #include <vnet/srv6/sr_packet.h>
29 :
30 : typedef enum
31 : {
32 : UDP_PING_NEXT_DROP,
33 : UDP_PING_NEXT_PUNT,
34 : UDP_PING_NEXT_UDP_LOOKUP,
35 : UDP_PING_NEXT_ICMP,
36 : UDP_PING_NEXT_IP6_LOOKUP,
37 : UDP_PING_NEXT_IP6_DROP,
38 : UDP_PING_N_NEXT,
39 : } udp_ping_next_t;
40 :
41 : #define foreach_udp_ping_error \
42 : _(BADHBH, "Malformed hop-by-hop header")
43 :
44 : typedef enum
45 : {
46 : #define _(sym,str) UDP_PING_ERROR_##sym,
47 : foreach_udp_ping_error
48 : #undef _
49 : UDP_PING_N_ERROR,
50 : } udp_ping_error_t;
51 :
52 : static char *udp_ping_error_strings[] = {
53 : #define _(sym,string) string,
54 : foreach_udp_ping_error
55 : #undef _
56 : };
57 :
58 : udp_ping_main_t udp_ping_main;
59 :
60 : uword
61 : udp_ping_process (vlib_main_t * vm,
62 : vlib_node_runtime_t * rt, vlib_frame_t * f);
63 :
64 : extern int
65 : ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
66 : ip6_hop_by_hop_option_t * opt);
67 :
68 : typedef struct
69 : {
70 : ip6_address_t src;
71 : ip6_address_t dst;
72 : u16 src_port;
73 : u16 dst_port;
74 : u16 handle;
75 : u16 next_index;
76 : u8 msg_type;
77 : } udp_ping_trace_t;
78 :
79 : /* packet trace format function */
80 : static u8 *
81 1 : format_udp_ping_trace (u8 * s, va_list * args)
82 : {
83 1 : CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84 1 : CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85 1 : udp_ping_trace_t *t = va_arg (*args, udp_ping_trace_t *);
86 :
87 1 : s = format (s, "udp-ping-local: src %U, dst %U, src_port %u, dst_port %u "
88 : "handle %u, next_index %u, msg_type %u",
89 : format_ip6_address, &t->src,
90 : format_ip6_address, &t->dst,
91 1 : t->src_port, t->dst_port,
92 1 : t->handle, t->next_index, t->msg_type);
93 1 : return s;
94 : }
95 :
96 : /* *INDENT-OFF* */
97 114668 : VLIB_REGISTER_NODE (udp_ping_node, static) =
98 : {
99 : .function = udp_ping_process,
100 : .type = VLIB_NODE_TYPE_PROCESS,
101 : .name = "udp-ping-process",
102 : };
103 : /* *INDENT-ON* */
104 :
105 : void
106 0 : udp_ping_calculate_timer_interval (void)
107 : {
108 : int i;
109 0 : ip46_udp_ping_flow *flow = NULL;
110 0 : u16 min_interval = 0x1e9;
111 :
112 0 : for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
113 : {
114 0 : if (pool_is_free_index (udp_ping_main.ip46_flow, i))
115 0 : continue;
116 :
117 0 : flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
118 :
119 0 : if (min_interval > flow->udp_data.interval)
120 0 : min_interval = flow->udp_data.interval;
121 : }
122 :
123 0 : if (udp_ping_main.timer_interval != min_interval)
124 : {
125 0 : udp_ping_main.timer_interval = min_interval;
126 0 : vlib_process_signal_event (udp_ping_main.vlib_main,
127 0 : udp_ping_node.index, EVENT_SIG_RECHECK, 0);
128 : }
129 0 : }
130 :
131 : void
132 0 : ip46_udp_ping_set_flow (ip46_address_t src, ip46_address_t dst,
133 : u16 start_src_port, u16 end_src_port,
134 : u16 start_dst_port, u16 end_dst_port,
135 : u16 interval, u8 fault_det, u8 is_disable)
136 : {
137 0 : u8 found = 0;
138 0 : ip46_udp_ping_flow *flow = NULL;
139 : int i;
140 :
141 0 : for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
142 : {
143 0 : if (pool_is_free_index (udp_ping_main.ip46_flow, i))
144 0 : continue;
145 :
146 0 : flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
147 0 : if ((0 == udp_ping_compare_flow (src, dst,
148 : start_src_port, end_src_port,
149 : start_dst_port, end_dst_port, flow)))
150 : {
151 0 : found = 1;
152 0 : break;
153 : }
154 : }
155 :
156 0 : if (found)
157 : {
158 : u16 cur_interval;
159 0 : if (is_disable)
160 : {
161 0 : cur_interval = flow->udp_data.interval;
162 0 : udp_ping_free_flow_data (flow);
163 0 : pool_put_index (udp_ping_main.ip46_flow, i);
164 0 : if (udp_ping_main.timer_interval == interval)
165 0 : udp_ping_calculate_timer_interval ();
166 0 : return;
167 : }
168 :
169 0 : cur_interval = flow->udp_data.interval;
170 0 : flow->udp_data.interval = interval;
171 0 : if (udp_ping_main.timer_interval > interval)
172 : {
173 0 : udp_ping_main.timer_interval = interval;
174 0 : vlib_process_signal_event (udp_ping_main.vlib_main,
175 0 : udp_ping_node.index,
176 : EVENT_SIG_RECHECK, 0);
177 : }
178 0 : else if (udp_ping_main.timer_interval == cur_interval)
179 0 : udp_ping_calculate_timer_interval ();
180 :
181 0 : return;
182 : }
183 :
184 : /* Delete operation and item not found */
185 0 : if (is_disable)
186 0 : return;
187 :
188 : /* Alloc new session */
189 0 : pool_get_aligned (udp_ping_main.ip46_flow, flow, CLIB_CACHE_LINE_BYTES);
190 0 : udp_ping_populate_flow (src, dst,
191 : start_src_port, end_src_port,
192 : start_dst_port, end_dst_port,
193 : interval, fault_det, flow);
194 :
195 0 : udp_ping_create_rewrite (flow, (flow - udp_ping_main.ip46_flow));
196 :
197 0 : if (udp_ping_main.timer_interval > interval)
198 : {
199 0 : udp_ping_main.timer_interval = interval;
200 0 : vlib_process_signal_event (udp_ping_main.vlib_main,
201 0 : udp_ping_node.index, EVENT_SIG_RECHECK, 0);
202 : }
203 0 : return;
204 : }
205 :
206 : uword
207 0 : unformat_port_range (unformat_input_t * input, va_list * args)
208 : {
209 : u16 *start_port, *end_port;
210 : uword c;
211 0 : u8 colon_present = 0;
212 :
213 0 : start_port = va_arg (*args, u16 *);
214 0 : end_port = va_arg (*args, u16 *);
215 :
216 0 : *start_port = *end_port = 0;
217 : /* Get start port */
218 0 : while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
219 : {
220 0 : switch (c)
221 : {
222 0 : case '0' ... '9':
223 0 : *start_port = ((*start_port) * 10) + (c - '0');
224 0 : break;
225 :
226 0 : case ':':
227 0 : colon_present = 1;
228 0 : break;
229 :
230 0 : default:
231 0 : return 0;
232 : }
233 :
234 0 : if (colon_present)
235 0 : break;
236 : }
237 :
238 0 : if (!colon_present)
239 0 : return 0;
240 :
241 : /* Get end port */
242 0 : while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
243 : {
244 0 : switch (c)
245 : {
246 0 : case '0' ... '9':
247 0 : *end_port = ((*end_port) * 10) + (c - '0');
248 0 : break;
249 :
250 0 : default:
251 0 : return 1;
252 : }
253 : }
254 :
255 0 : if (end_port < start_port)
256 0 : return 0;
257 :
258 0 : return 1;
259 : }
260 :
261 : static clib_error_t *
262 0 : set_udp_ping_command_fn (vlib_main_t * vm,
263 : unformat_input_t * input, vlib_cli_command_t * cmd)
264 : {
265 : ip46_address_t dst, src;
266 : u16 start_src_port, end_src_port;
267 : u16 start_dst_port, end_dst_port;
268 : u32 interval;
269 0 : u8 is_disable = 0;
270 0 : u8 fault_det = 0;
271 :
272 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
273 : {
274 0 : if (unformat
275 : (input, "src %U", unformat_ip46_address, &src, IP46_TYPE_ANY))
276 : ;
277 0 : else if (unformat (input, "src-port-range %U",
278 : unformat_port_range, &start_src_port, &end_src_port))
279 : ;
280 : else
281 0 : if (unformat
282 : (input, "dst %U", unformat_ip46_address, &dst, IP46_TYPE_ANY))
283 : ;
284 0 : else if (unformat (input, "dst-port-range %U",
285 : unformat_port_range, &start_dst_port, &end_dst_port))
286 : ;
287 0 : else if (unformat (input, "interval %d", &interval))
288 : ;
289 0 : else if (unformat (input, "fault-detect"))
290 0 : fault_det = 1;
291 0 : else if (unformat (input, "disable"))
292 0 : is_disable = 1;
293 : else
294 0 : break;
295 : }
296 :
297 0 : ip46_udp_ping_set_flow (src, dst, start_src_port, end_src_port,
298 0 : start_dst_port, end_dst_port, (u16) interval,
299 : fault_det, is_disable);
300 :
301 0 : return 0;
302 : }
303 :
304 : /* *INDENT-OFF* */
305 186217 : VLIB_CLI_COMMAND (set_udp_ping_command, static) =
306 : {
307 : .path = "set udp-ping",
308 : .short_help =
309 : "set udp-ping src <local IPv6 address> src-port-range <local port range> \
310 : dst <remote IPv6 address> dst-port-range <destination port range> \
311 : interval <time interval in sec for which ping packet will be sent> \
312 : [disable]",
313 : .function = set_udp_ping_command_fn,
314 : };
315 : /* *INDENT-ON* */
316 :
317 : static clib_error_t *
318 0 : show_udp_ping_summary_cmd_fn (vlib_main_t * vm,
319 : unformat_input_t * input,
320 : vlib_cli_command_t * cmd)
321 : {
322 0 : u8 *s = 0;
323 : int i, j;
324 : ip46_udp_ping_flow *ip46_flow;
325 : u16 src_port, dst_port;
326 : udp_ping_flow_data *stats;
327 :
328 0 : s = format (s, "UDP-Ping data:\n");
329 :
330 0 : for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
331 : {
332 0 : if (pool_is_free_index (udp_ping_main.ip46_flow, i))
333 0 : continue;
334 :
335 0 : ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
336 0 : s = format (s, "Src: %U, Dst: %U\n",
337 : format_ip46_address, &ip46_flow->src, IP46_TYPE_ANY,
338 : format_ip46_address, &ip46_flow->dst, IP46_TYPE_ANY);
339 :
340 0 : s = format (s, "Start src port: %u, End src port: %u\n",
341 0 : ip46_flow->udp_data.start_src_port,
342 0 : ip46_flow->udp_data.end_src_port);
343 0 : s = format (s, "Start dst port: %u, End dst port: %u\n",
344 0 : ip46_flow->udp_data.start_dst_port,
345 0 : ip46_flow->udp_data.end_dst_port);
346 0 : s = format (s, "Interval: %u\n", ip46_flow->udp_data.interval);
347 :
348 0 : j = 0;
349 0 : for (src_port = ip46_flow->udp_data.start_src_port;
350 0 : src_port <= ip46_flow->udp_data.end_src_port; src_port++)
351 : {
352 0 : for (dst_port = ip46_flow->udp_data.start_dst_port;
353 0 : dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++)
354 : {
355 0 : stats = ip46_flow->udp_data.stats + j;
356 0 : s =
357 0 : format (s, "\nSrc Port - %u, Dst Port - %u, Flow CTX - %u\n",
358 : src_port, dst_port, stats->flow_ctx);
359 0 : s =
360 0 : format (s, "Path State - %s\n",
361 0 : (stats->retry > MAX_PING_RETRIES) ? "Down" : "Up");
362 0 : s = format (s, "Path Data:\n");
363 0 : s = print_analyse_flow (s,
364 0 : &ip46_flow->udp_data.
365 0 : stats[j].analyse_data);
366 0 : j++;
367 : }
368 : }
369 0 : s = format (s, "\n\n");
370 : }
371 :
372 0 : vlib_cli_output (vm, "%v", s);
373 0 : vec_free (s);
374 0 : return 0;
375 : }
376 :
377 : /* *INDENT-OFF* */
378 186217 : VLIB_CLI_COMMAND (show_udp_ping_cmd, static) =
379 : {
380 : .path = "show udp-ping summary",
381 : .short_help = "Summary of udp-ping",
382 : .function = show_udp_ping_summary_cmd_fn,
383 : };
384 : /* *INDENT-ON* */
385 :
386 : /**
387 : * @brief UDP-Ping Process node.
388 : * @node udp-ping-process
389 : *
390 : * This is process node which wakes up when periodically to send
391 : * out udp probe packets for all configured sessions.
392 : *
393 : * @param vm vlib_main_t corresponding to the current thread.
394 : * @param rt vlib_node_runtime_t data for this node.
395 : * @param f vlib_frame_t whose contents should be dispatched.
396 : *
397 : */
398 : uword
399 575 : udp_ping_process (vlib_main_t * vm,
400 : vlib_node_runtime_t * rt, vlib_frame_t * f)
401 : {
402 : f64 now;
403 575 : uword *event_data = 0;
404 : int i;
405 : ip46_udp_ping_flow *ip46_flow;
406 :
407 : while (1)
408 : {
409 575 : vec_reset_length (event_data);
410 575 : vlib_process_wait_for_event_or_clock (vm, udp_ping_main.timer_interval);
411 0 : (void) vlib_process_get_events (vm, &event_data);
412 0 : now = vlib_time_now (vm);
413 :
414 0 : for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
415 : {
416 0 : if (pool_is_free_index (udp_ping_main.ip46_flow, i))
417 0 : continue;
418 :
419 0 : ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
420 0 : if (ip46_flow->udp_data.next_send_time < now)
421 0 : udp_ping_send_ip6_pak (udp_ping_main.vlib_main, ip46_flow);
422 : }
423 : }
424 : return 0;
425 : }
426 :
427 : /**
428 : * @brief HopByHop analyse function for udp-ping response.
429 : *
430 : * Walks through all hbh options present in udp-ping response
431 : * and uses analyser library for the analysis.
432 : *
433 : */
434 : void
435 0 : udp_ping_analyse_hbh (vlib_buffer_t * b0,
436 : u32 flow_id,
437 : u16 src_port,
438 : u16 dst_port,
439 : ip6_hop_by_hop_option_t * opt0,
440 : ip6_hop_by_hop_option_t * limit0, u16 len)
441 : {
442 : u8 type0;
443 : ip46_udp_ping_flow *ip46_flow;
444 : u16 flow_index;
445 : ioam_analyser_data_t *data;
446 : ioam_e2e_option_t *e2e;
447 : ioam_trace_option_t *trace;
448 :
449 : /* If the packet doesnt match UDP session then return */
450 0 : if (PREDICT_FALSE (pool_is_free_index (udp_ping_main.ip46_flow, flow_id)))
451 0 : return;
452 :
453 0 : ip46_flow = udp_ping_main.ip46_flow + flow_id;
454 : /* Check port is within range */
455 0 : if (PREDICT_FALSE ((src_port < ip46_flow->udp_data.start_src_port) ||
456 : (src_port > ip46_flow->udp_data.end_src_port) ||
457 : (dst_port < ip46_flow->udp_data.start_dst_port) ||
458 : (dst_port > ip46_flow->udp_data.end_dst_port)))
459 0 : return;
460 :
461 0 : flow_index = (src_port - ip46_flow->udp_data.start_src_port) *
462 0 : (ip46_flow->udp_data.end_dst_port - ip46_flow->udp_data.start_dst_port +
463 : 1);
464 0 : flow_index += (dst_port - ip46_flow->udp_data.start_dst_port);
465 0 : data = &(ip46_flow->udp_data.stats[flow_index].analyse_data);
466 :
467 0 : data->pkt_counter++;
468 0 : data->bytes_counter += len;
469 :
470 0 : vnet_buffer (b0)->l2_classify.opaque_index =
471 0 : ip46_flow->udp_data.stats[flow_index].flow_ctx;
472 :
473 0 : while (opt0 < limit0)
474 : {
475 0 : type0 = opt0->type;
476 0 : switch (type0)
477 : {
478 0 : case HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST:
479 : /* Add trace for here as it hasnt been done yet */
480 0 : vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
481 0 : trace = (ioam_trace_option_t *) opt0;
482 0 : if (PREDICT_FALSE
483 : (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY))
484 : {
485 0 : ip6_ioam_analyse_hbh_trace_loopback (data, &trace->trace_hdr,
486 0 : (trace->hdr.length - 2));
487 0 : return;
488 : }
489 0 : ip6_hbh_ioam_trace_data_list_handler (b0,
490 0 : vlib_buffer_get_current (b0),
491 : opt0);
492 0 : (void) ip6_ioam_analyse_hbh_trace (data, &trace->trace_hdr, len,
493 0 : (trace->hdr.length - 2));
494 0 : break;
495 0 : case HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE:
496 0 : e2e = (ioam_e2e_option_t *) opt0;
497 0 : (void) ip6_ioam_analyse_hbh_e2e (data, &e2e->e2e_hdr, len);
498 0 : break;
499 0 : case 0: /* Pad1 */
500 0 : opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
501 0 : continue;
502 0 : case 1: /* PadN */
503 0 : break;
504 0 : default:
505 0 : break;
506 : }
507 0 : opt0 = (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
508 : sizeof (ip6_hop_by_hop_option_t));
509 : }
510 0 : ip46_flow->udp_data.stats[flow_index].retry = 0;
511 : }
512 :
513 : /**
514 : * @brief UDP-Ping request/response handler function.
515 : *
516 : * Checks udp-ping packet type - request/response and handles them.
517 : * If not udp-ping packet then, strips off hbh options and enques
518 : * packet to protocol registered node to enable next protocol processing.
519 : *
520 : */
521 : void
522 1 : udp_ping_local_analyse (vlib_node_runtime_t * node, vlib_buffer_t * b0,
523 : ip6_header_t * ip0, ip6_hop_by_hop_header_t * hbh0,
524 : u16 * next0)
525 : {
526 1 : ip6_main_t *im = &ip6_main;
527 1 : ip_lookup_main_t *lm = &im->lookup_main;
528 :
529 1 : *next0 = UDP_PING_NEXT_IP6_DROP;
530 :
531 : /*
532 : * Sanity check: hbh header length must be less than
533 : * b0->current_length.
534 : */
535 1 : if (PREDICT_FALSE ((hbh0->length + 1) << 3) >= b0->current_length)
536 : {
537 0 : *next0 = UDP_PING_NEXT_DROP;
538 0 : b0->error = node->errors[UDP_PING_ERROR_BADHBH];
539 0 : return;
540 : }
541 :
542 1 : if (PREDICT_TRUE (hbh0->protocol == IP_PROTOCOL_UDP))
543 : {
544 : ip6_hop_by_hop_option_t *opt0;
545 : ip6_hop_by_hop_option_t *limit0;
546 : u16 p_len0;
547 : udp_ping_t *udp0;
548 :
549 : /* Check for udp ping packet */
550 1 : udp0 = (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
551 1 : opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
552 1 : if ((udp0->ping_data.probe_marker1 ==
553 1 : clib_host_to_net_u32 (UDP_PING_PROBE_MARKER1)) &&
554 0 : (udp0->ping_data.probe_marker2 ==
555 0 : clib_host_to_net_u32 (UDP_PING_PROBE_MARKER2)))
556 : {
557 0 : if (udp0->ping_data.msg_type == UDP_PING_PROBE)
558 : {
559 0 : udp_ping_create_reply_from_probe_ip6 (ip0, hbh0, udp0);
560 : /* Skip e2e processing */
561 0 : vnet_buffer (b0)->l2_classify.opaque_index = 0x7FFFFFFF;
562 0 : *next0 = UDP_PING_NEXT_IP6_LOOKUP;
563 0 : return;
564 : }
565 :
566 : /* Reply */
567 0 : opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
568 0 : limit0 = (ip6_hop_by_hop_option_t *)
569 0 : ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
570 0 : p_len0 = clib_net_to_host_u16 (ip0->payload_length);
571 0 : udp_ping_analyse_hbh (b0,
572 0 : clib_net_to_host_u16 (udp0->
573 0 : ping_data.sender_handle),
574 0 : clib_net_to_host_u16 (udp0->udp.dst_port),
575 0 : clib_net_to_host_u16 (udp0->udp.src_port),
576 : opt0, limit0, p_len0);
577 :
578 : /* UDP Ping packet, so return */
579 0 : return;
580 : }
581 : }
582 :
583 : /* If next header is SR, then destination may get overwritten to
584 : * remote address. So pass it to SR processing as it may be local packet
585 : * afterall
586 : */
587 1 : if (PREDICT_FALSE (hbh0->protocol == IPPROTO_IPV6_ROUTE))
588 0 : goto end;
589 :
590 : /* Other case remove hbh-ioam headers */
591 : u64 *copy_dst0, *copy_src0;
592 : u16 new_l0;
593 :
594 1 : vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
595 :
596 1 : new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
597 1 : ((hbh0->length + 1) << 3);
598 :
599 1 : ip0->payload_length = clib_host_to_net_u16 (new_l0);
600 :
601 1 : ip0->protocol = hbh0->protocol;
602 :
603 1 : copy_src0 = (u64 *) ip0;
604 1 : copy_dst0 = copy_src0 + (hbh0->length + 1);
605 1 : copy_dst0[4] = copy_src0[4];
606 1 : copy_dst0[3] = copy_src0[3];
607 1 : copy_dst0[2] = copy_src0[2];
608 1 : copy_dst0[1] = copy_src0[1];
609 1 : copy_dst0[0] = copy_src0[0];
610 :
611 1 : end:
612 1 : *next0 = lm->local_next_by_ip_protocol[hbh0->protocol];
613 1 : return;
614 : }
615 :
616 : /**
617 : * @brief udp ping request/response packet receive node.
618 : * @node udp-ping-local
619 : *
620 : * This function receives udp ping request/response packets and process them.
621 : * For request packets, response is created and sent.
622 : * For response packets, they are analysed and results stored.
623 : *
624 : * @param vm vlib_main_t corresponding to the current thread.
625 : * @param node vlib_node_runtime_t data for this node.
626 : * @param frame vlib_frame_t whose contents should be dispatched.
627 : *
628 : * @par Graph mechanics: buffer, next index usage
629 : *
630 : * <em>Uses:</em>
631 : * - <code>udp_ping_local_analyse(node, p0, ip0, hbh0, &next0)</code>
632 : * - Checks packet type - request/respnse and process them.
633 : *
634 : * <em>Next Index:</em>
635 : * - Dispatches the packet to ip6-lookup/ip6-drop depending on type of packet.
636 : */
637 : static uword
638 1 : udp_ping_local_node_fn (vlib_main_t * vm,
639 : vlib_node_runtime_t * node, vlib_frame_t * frame)
640 : {
641 : udp_ping_next_t next_index;
642 : u32 *from, *to_next, n_left_from, n_left_to_next;
643 :
644 1 : from = vlib_frame_vector_args (frame);
645 1 : n_left_from = frame->n_vectors;
646 1 : next_index = node->cached_next_index;
647 :
648 2 : while (n_left_from > 0)
649 : {
650 1 : vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
651 :
652 1 : while (n_left_from >= 4 && n_left_to_next >= 2)
653 : {
654 : vlib_buffer_t *p0, *p1;
655 : ip6_header_t *ip0, *ip1;
656 : ip6_hop_by_hop_header_t *hbh0, *hbh1;
657 : u16 next0, next1;
658 : u32 pi0, pi1;
659 :
660 : /* Prefetch next iteration. */
661 : {
662 : vlib_buffer_t *p2, *p3;
663 :
664 0 : p2 = vlib_get_buffer (vm, from[2]);
665 0 : p3 = vlib_get_buffer (vm, from[3]);
666 :
667 0 : vlib_prefetch_buffer_header (p2, LOAD);
668 0 : vlib_prefetch_buffer_header (p3, LOAD);
669 :
670 : /* Prefetch 3 cache lines as we need to look deep into packet */
671 0 : CLIB_PREFETCH (p2->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
672 0 : CLIB_PREFETCH (p3->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
673 : }
674 :
675 0 : pi0 = to_next[0] = from[0];
676 0 : pi1 = to_next[1] = from[1];
677 0 : from += 2;
678 0 : n_left_from -= 2;
679 0 : to_next += 2;
680 0 : n_left_to_next -= 2;
681 :
682 0 : p0 = vlib_get_buffer (vm, pi0);
683 0 : p1 = vlib_get_buffer (vm, pi1);
684 :
685 0 : ip0 = vlib_buffer_get_current (p0);
686 0 : ip1 = vlib_buffer_get_current (p1);
687 :
688 0 : hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
689 0 : hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
690 :
691 0 : udp_ping_local_analyse (node, p0, ip0, hbh0, &next0);
692 0 : udp_ping_local_analyse (node, p1, ip1, hbh1, &next1);
693 :
694 0 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
695 : {
696 0 : if (p0->flags & VLIB_BUFFER_IS_TRACED)
697 : {
698 : udp_ping_trace_t *t0 =
699 0 : vlib_add_trace (vm, node, p0, sizeof (*t0));
700 : udp_ping_t *udp0;
701 :
702 : /* Check for udp ping packet */
703 0 : udp0 =
704 0 : (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
705 0 : t0->src = ip0->src_address;
706 0 : t0->dst = ip0->dst_address;
707 0 : t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
708 0 : t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
709 0 : t0->handle =
710 0 : clib_net_to_host_u16 (udp0->ping_data.sender_handle);
711 0 : t0->msg_type = udp0->ping_data.msg_type;
712 0 : t0->next_index = next0;
713 : }
714 0 : if (p1->flags & VLIB_BUFFER_IS_TRACED)
715 : {
716 : udp_ping_trace_t *t1 =
717 0 : vlib_add_trace (vm, node, p1, sizeof (*t1));
718 : udp_ping_t *udp1;
719 :
720 : /* Check for udp ping packet */
721 0 : udp1 =
722 0 : (udp_ping_t *) ((u8 *) hbh1 + ((hbh1->length + 1) << 3));
723 0 : t1->src = ip1->src_address;
724 0 : t1->dst = ip1->dst_address;
725 0 : t1->src_port = clib_net_to_host_u16 (udp1->udp.src_port);
726 0 : t1->dst_port = clib_net_to_host_u16 (udp1->udp.dst_port);
727 0 : t1->handle =
728 0 : clib_net_to_host_u16 (udp1->ping_data.sender_handle);
729 0 : t1->msg_type = udp1->ping_data.msg_type;
730 0 : t1->next_index = next1;
731 : }
732 : }
733 :
734 0 : vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
735 : to_next, n_left_to_next,
736 : pi0, pi1, next0, next1);
737 : }
738 :
739 2 : while (n_left_from > 0 && n_left_to_next > 0)
740 : {
741 : vlib_buffer_t *p0;
742 : ip6_header_t *ip0;
743 : ip6_hop_by_hop_header_t *hbh0;
744 : u16 next0;
745 : u32 pi0;
746 :
747 1 : pi0 = from[0];
748 1 : to_next[0] = pi0;
749 1 : from += 1;
750 1 : to_next += 1;
751 1 : n_left_from -= 1;
752 1 : n_left_to_next -= 1;
753 :
754 1 : p0 = vlib_get_buffer (vm, pi0);
755 1 : ip0 = vlib_buffer_get_current (p0);
756 1 : hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
757 :
758 1 : udp_ping_local_analyse (node, p0, ip0, hbh0, &next0);
759 :
760 1 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
761 : {
762 1 : if (p0->flags & VLIB_BUFFER_IS_TRACED)
763 : {
764 : udp_ping_trace_t *t0 =
765 1 : vlib_add_trace (vm, node, p0, sizeof (*t0));
766 : udp_ping_t *udp0;
767 :
768 : /* Check for udp ping packet */
769 1 : udp0 =
770 1 : (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
771 1 : t0->src = ip0->src_address;
772 1 : t0->dst = ip0->dst_address;
773 1 : t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
774 1 : t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
775 1 : t0->handle =
776 1 : clib_net_to_host_u16 (udp0->ping_data.sender_handle);
777 1 : t0->msg_type = udp0->ping_data.msg_type;
778 1 : t0->next_index = next0;
779 : }
780 : }
781 :
782 1 : vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
783 : to_next, n_left_to_next,
784 : pi0, next0);
785 : }
786 :
787 1 : vlib_put_next_frame (vm, node, next_index, n_left_to_next);
788 : }
789 :
790 1 : return frame->n_vectors;
791 : }
792 :
793 : /* *INDENT-OFF* */
794 : /*
795 : * Node for udp-ping-local
796 : */
797 114668 : VLIB_REGISTER_NODE (udp_ping_local, static) =
798 : {
799 : .function = udp_ping_local_node_fn,
800 : .name = "udp-ping-local",
801 : .vector_size = sizeof (u32),
802 : .format_trace = format_udp_ping_trace,
803 : .type = VLIB_NODE_TYPE_INTERNAL,
804 : .n_next_nodes = UDP_PING_N_NEXT,
805 : .n_errors = UDP_PING_N_ERROR,
806 : .error_strings = udp_ping_error_strings,
807 : .next_nodes =
808 : {
809 : [UDP_PING_NEXT_DROP] = "error-drop",
810 : [UDP_PING_NEXT_PUNT] = "error-punt",
811 : [UDP_PING_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
812 : [UDP_PING_NEXT_ICMP] = "ip6-icmp-input",
813 : [UDP_PING_NEXT_IP6_LOOKUP] = "ip6-lookup",
814 : [UDP_PING_NEXT_IP6_DROP] = "ip6-drop",
815 : },
816 : };
817 : /* *INDENT-ON* */
818 :
819 : static clib_error_t *
820 575 : udp_ping_init (vlib_main_t * vm)
821 : {
822 575 : udp_ping_main.vlib_main = vm;
823 575 : udp_ping_main.vnet_main = vnet_get_main ();
824 575 : udp_ping_main.timer_interval = 1e9;
825 :
826 575 : ip6_local_hop_by_hop_register_protocol (IP_PROTOCOL_UDP,
827 : udp_ping_local.index);
828 575 : return 0;
829 : }
830 :
831 : /* *INDENT-OFF* */
832 8063 : VLIB_INIT_FUNCTION (udp_ping_init) =
833 : {
834 : .runs_after = VLIB_INITS("ip_main_init"),
835 : };
836 : /* *INDENT-ON* */
837 :
838 : /*
839 : * fd.io coding-style-patch-verification: ON
840 : *
841 : * Local Variables:
842 : * eval: (c-set-style "gnu")
843 : * End:
844 : */
|