Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vnet/udp/udp.h> |
| 6 | #include <vnet/ip/ip4_inlines.h> |
| 7 | #include <vnet/ip/ip6_inlines.h> |
| 8 | |
| 9 | #define udp_node_index(node_id, is_ip4) \ |
| 10 | ((is_ip4) ? udp4_##node_id##_node.index : udp6_##node_id##_node.index) |
| 11 | |
| 12 | typedef enum udp_output_next_ |
| 13 | { |
| 14 | UDP_OUTPUT_NEXT_DROP, |
| 15 | UDP_OUTPUT_NEXT_IP_LOOKUP, |
| 16 | UDP_OUTPUT_N_NEXT |
| 17 | } udp_output_next_t; |
| 18 | |
| 19 | #define foreach_udp4_output_next \ |
| 20 | _ (DROP, "error-drop") \ |
| 21 | _ (IP_LOOKUP, "ip4-lookup") |
| 22 | |
| 23 | #define foreach_udp6_output_next \ |
| 24 | _ (DROP, "error-drop") \ |
| 25 | _ (IP_LOOKUP, "ip6-lookup") |
| 26 | |
| 27 | static vlib_error_desc_t udp_output_error_counters[] = { |
| 28 | #define udp_error(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s }, |
| 29 | #include <vnet/udp/udp_error.def> |
| 30 | #undef udp_error |
| 31 | }; |
| 32 | |
| 33 | typedef struct udp_tx_trace_ |
| 34 | { |
| 35 | udp_header_t udp_header; |
| 36 | udp_connection_t udp_connection; |
| 37 | } udp_tx_trace_t; |
| 38 | |
| 39 | static u8 * |
| 40 | format_udp_tx_trace (u8 *s, va_list *args) |
| 41 | { |
| 42 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 43 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 44 | udp_tx_trace_t *t = va_arg (*args, udp_tx_trace_t *); |
| 45 | udp_connection_t *uc = &t->udp_connection; |
| 46 | u32 indent = format_get_indent (s); |
| 47 | |
| 48 | s = format (s, "%U\n%U%U", format_udp_connection, uc, format_white_space, |
| 49 | indent, format_udp_header, &t->udp_header, 128); |
| 50 | |
| 51 | return s; |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | udp46_output_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 56 | u32 *to_next, u32 n_bufs) |
| 57 | { |
| 58 | udp_connection_t *uc; |
| 59 | udp_tx_trace_t *t; |
| 60 | vlib_buffer_t *b; |
| 61 | udp_header_t *uh; |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < n_bufs; i++) |
| 65 | { |
| 66 | b = vlib_get_buffer (vm, to_next[i]); |
| 67 | if (!(b->flags & VLIB_BUFFER_IS_TRACED)) |
| 68 | continue; |
| 69 | uh = vlib_buffer_get_current (b); |
| 70 | uc = udp_connection_get (vnet_buffer (b)->tcp.connection_index, |
| 71 | vm->thread_index); |
| 72 | t = vlib_add_trace (vm, node, b, sizeof (*t)); |
| 73 | clib_memcpy_fast (&t->udp_header, uh, sizeof (t->udp_header)); |
| 74 | clib_memcpy_fast (&t->udp_connection, uc, sizeof (t->udp_connection)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | always_inline void |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 79 | udp_output_handle_packet (udp_connection_t *uc0, vlib_buffer_t *b0, |
| 80 | vlib_node_runtime_t *error_node, u16 *next0, |
| 81 | u8 is_ip4) |
| 82 | { |
| 83 | /* If next_index is not drop use it */ |
| 84 | if (uc0->next_node_index) |
| 85 | { |
| 86 | *next0 = uc0->next_node_index; |
| 87 | vnet_buffer (b0)->tcp.next_node_opaque = uc0->next_node_opaque; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | *next0 = UDP_OUTPUT_NEXT_IP_LOOKUP; |
| 92 | } |
| 93 | |
| 94 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = uc0->c_fib_index; |
| 95 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = uc0->sw_if_index; |
| 96 | } |
| 97 | |
Florin Coras | 15952b2 | 2022-12-19 10:55:18 -0800 | [diff] [blame] | 98 | always_inline udp_connection_t * |
| 99 | udp_output_get_connection (vlib_buffer_t *b, u32 thread_index) |
| 100 | { |
| 101 | if (PREDICT_FALSE (vnet_buffer (b)->tcp.flags & UDP_CONN_F_LISTEN)) |
| 102 | return udp_listener_get (vnet_buffer (b)->tcp.connection_index); |
| 103 | |
| 104 | return udp_connection_get (vnet_buffer (b)->tcp.connection_index, |
| 105 | thread_index); |
| 106 | } |
| 107 | |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 108 | always_inline uword |
| 109 | udp46_output_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 110 | vlib_frame_t *frame, int is_ip4) |
| 111 | { |
| 112 | u32 n_left_from, *from, thread_index = vm->thread_index; |
| 113 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 114 | u16 nexts[VLIB_FRAME_SIZE], *next; |
| 115 | |
| 116 | from = vlib_frame_vector_args (frame); |
| 117 | n_left_from = frame->n_vectors; |
| 118 | |
| 119 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) |
| 120 | udp46_output_trace_frame (vm, node, from, n_left_from); |
| 121 | |
| 122 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 123 | b = bufs; |
| 124 | next = nexts; |
| 125 | |
| 126 | while (n_left_from >= 4) |
| 127 | { |
| 128 | udp_connection_t *uc0, *uc1; |
| 129 | |
| 130 | vlib_prefetch_buffer_header (b[2], STORE); |
| 131 | CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE); |
| 132 | |
| 133 | vlib_prefetch_buffer_header (b[3], STORE); |
| 134 | CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE); |
| 135 | |
Florin Coras | 15952b2 | 2022-12-19 10:55:18 -0800 | [diff] [blame] | 136 | uc0 = udp_output_get_connection (b[0], thread_index); |
| 137 | uc1 = udp_output_get_connection (b[1], thread_index); |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 138 | |
| 139 | if (PREDICT_TRUE (!uc0 + !uc1 == 0)) |
| 140 | { |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 141 | udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4); |
| 142 | udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | if (uc0 != 0) |
| 147 | { |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 148 | udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | b[0]->error = node->errors[UDP_ERROR_INVALID_CONNECTION]; |
| 153 | next[0] = UDP_OUTPUT_NEXT_DROP; |
| 154 | } |
| 155 | if (uc1 != 0) |
| 156 | { |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 157 | udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | b[1]->error = node->errors[UDP_ERROR_INVALID_CONNECTION]; |
| 162 | next[1] = UDP_OUTPUT_NEXT_DROP; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | b += 2; |
| 167 | next += 2; |
| 168 | n_left_from -= 2; |
| 169 | } |
| 170 | while (n_left_from > 0) |
| 171 | { |
| 172 | udp_connection_t *uc0; |
| 173 | |
| 174 | if (n_left_from > 1) |
| 175 | { |
| 176 | vlib_prefetch_buffer_header (b[1], STORE); |
| 177 | CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE); |
| 178 | } |
| 179 | |
Florin Coras | 15952b2 | 2022-12-19 10:55:18 -0800 | [diff] [blame] | 180 | uc0 = udp_output_get_connection (b[0], thread_index); |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 181 | |
| 182 | if (PREDICT_TRUE (uc0 != 0)) |
| 183 | { |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 184 | udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | b[0]->error = node->errors[UDP_ERROR_INVALID_CONNECTION]; |
| 189 | next[0] = UDP_OUTPUT_NEXT_DROP; |
| 190 | } |
| 191 | |
| 192 | b += 1; |
| 193 | next += 1; |
| 194 | n_left_from -= 1; |
| 195 | } |
| 196 | |
| 197 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 198 | vlib_node_increment_counter (vm, udp_node_index (output, is_ip4), |
| 199 | UDP_ERROR_PKTS_SENT, frame->n_vectors); |
| 200 | return frame->n_vectors; |
| 201 | } |
| 202 | |
| 203 | VLIB_NODE_FN (udp4_output_node) |
| 204 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 205 | { |
| 206 | return udp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */); |
| 207 | } |
| 208 | |
| 209 | VLIB_NODE_FN (udp6_output_node) |
| 210 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 211 | { |
| 212 | return udp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */); |
| 213 | } |
| 214 | |
| 215 | VLIB_REGISTER_NODE (udp4_output_node) = |
| 216 | { |
| 217 | .name = "udp4-output", |
| 218 | .vector_size = sizeof (u32), |
| 219 | .n_errors = UDP_N_ERROR, |
| 220 | .protocol_hint = VLIB_NODE_PROTO_HINT_UDP, |
| 221 | .error_counters = udp_output_error_counters, |
| 222 | .n_next_nodes = UDP_OUTPUT_N_NEXT, |
| 223 | .next_nodes = { |
| 224 | #define _(s, n) [UDP_OUTPUT_NEXT_##s] = n, |
| 225 | foreach_udp4_output_next |
| 226 | #undef _ |
| 227 | }, |
| 228 | .format_buffer = format_udp_header, |
| 229 | .format_trace = format_udp_tx_trace, |
| 230 | }; |
| 231 | |
| 232 | VLIB_REGISTER_NODE (udp6_output_node) = |
| 233 | { |
| 234 | .name = "udp6-output", |
| 235 | .vector_size = sizeof (u32), |
| 236 | .n_errors = UDP_N_ERROR, |
| 237 | .protocol_hint = VLIB_NODE_PROTO_HINT_UDP, |
| 238 | .error_counters = udp_output_error_counters, |
| 239 | .n_next_nodes = UDP_OUTPUT_N_NEXT, |
| 240 | .next_nodes = { |
| 241 | #define _(s, n) [UDP_OUTPUT_NEXT_##s] = n, |
| 242 | foreach_udp6_output_next |
| 243 | #undef _ |
| 244 | }, |
| 245 | .format_buffer = format_udp_header, |
| 246 | .format_trace = format_udp_tx_trace, |
| 247 | }; |
| 248 | |
| 249 | /* |
| 250 | * fd.io coding-style-patch-verification: ON |
| 251 | * |
| 252 | * Local Variables: |
| 253 | * eval: (c-set-style "gnu") |
| 254 | * End: |
| 255 | */ |