udp: support for disabling tx csum
[vpp.git] / src / vnet / udp / udp_output.c
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
79 udp_output_push_ip (vlib_main_t *vm, vlib_buffer_t *b, udp_connection_t *uc,
80                     u8 is_ip4)
81 {
82   if (uc->c_is_ip4)
83     vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
84                                  IP_PROTOCOL_UDP, udp_csum_offload (uc),
85                                  0 /* is_df */, uc->c_dscp);
86   else
87     vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
88                           IP_PROTOCOL_UDP);
89   vnet_buffer (b)->sw_if_index[VLIB_RX] = uc->sw_if_index;
90   vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
91 }
92
93 always_inline void
94 udp_output_handle_packet (udp_connection_t *uc0, vlib_buffer_t *b0,
95                           vlib_node_runtime_t *error_node, u16 *next0,
96                           u8 is_ip4)
97 {
98   /* If next_index is not drop use it */
99   if (uc0->next_node_index)
100     {
101       *next0 = uc0->next_node_index;
102       vnet_buffer (b0)->tcp.next_node_opaque = uc0->next_node_opaque;
103     }
104   else
105     {
106       *next0 = UDP_OUTPUT_NEXT_IP_LOOKUP;
107     }
108
109   vnet_buffer (b0)->sw_if_index[VLIB_TX] = uc0->c_fib_index;
110   vnet_buffer (b0)->sw_if_index[VLIB_RX] = uc0->sw_if_index;
111 }
112
113 always_inline uword
114 udp46_output_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
115                      vlib_frame_t *frame, int is_ip4)
116 {
117   u32 n_left_from, *from, thread_index = vm->thread_index;
118   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
119   u16 nexts[VLIB_FRAME_SIZE], *next;
120
121   from = vlib_frame_vector_args (frame);
122   n_left_from = frame->n_vectors;
123
124   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
125     udp46_output_trace_frame (vm, node, from, n_left_from);
126
127   vlib_get_buffers (vm, from, bufs, n_left_from);
128   b = bufs;
129   next = nexts;
130
131   while (n_left_from >= 4)
132     {
133       udp_connection_t *uc0, *uc1;
134
135       vlib_prefetch_buffer_header (b[2], STORE);
136       CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
137
138       vlib_prefetch_buffer_header (b[3], STORE);
139       CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
140
141       uc0 = udp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
142                                 thread_index);
143       uc1 = udp_connection_get (vnet_buffer (b[1])->tcp.connection_index,
144                                 thread_index);
145
146       if (PREDICT_TRUE (!uc0 + !uc1 == 0))
147         {
148           udp_output_push_ip (vm, b[0], uc0, is_ip4);
149           udp_output_push_ip (vm, b[1], uc1, is_ip4);
150
151           udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
152           udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4);
153         }
154       else
155         {
156           if (uc0 != 0)
157             {
158               udp_output_push_ip (vm, b[0], uc0, is_ip4);
159               udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
160             }
161           else
162             {
163               b[0]->error = node->errors[UDP_ERROR_INVALID_CONNECTION];
164               next[0] = UDP_OUTPUT_NEXT_DROP;
165             }
166           if (uc1 != 0)
167             {
168               udp_output_push_ip (vm, b[1], uc1, is_ip4);
169               udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4);
170             }
171           else
172             {
173               b[1]->error = node->errors[UDP_ERROR_INVALID_CONNECTION];
174               next[1] = UDP_OUTPUT_NEXT_DROP;
175             }
176         }
177
178       b += 2;
179       next += 2;
180       n_left_from -= 2;
181     }
182   while (n_left_from > 0)
183     {
184       udp_connection_t *uc0;
185
186       if (n_left_from > 1)
187         {
188           vlib_prefetch_buffer_header (b[1], STORE);
189           CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
190         }
191
192       uc0 = udp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
193                                 thread_index);
194
195       if (PREDICT_TRUE (uc0 != 0))
196         {
197           udp_output_push_ip (vm, b[0], uc0, is_ip4);
198           udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
199         }
200       else
201         {
202           b[0]->error = node->errors[UDP_ERROR_INVALID_CONNECTION];
203           next[0] = UDP_OUTPUT_NEXT_DROP;
204         }
205
206       b += 1;
207       next += 1;
208       n_left_from -= 1;
209     }
210
211   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
212   vlib_node_increment_counter (vm, udp_node_index (output, is_ip4),
213                                UDP_ERROR_PKTS_SENT, frame->n_vectors);
214   return frame->n_vectors;
215 }
216
217 VLIB_NODE_FN (udp4_output_node)
218 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
219 {
220   return udp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */);
221 }
222
223 VLIB_NODE_FN (udp6_output_node)
224 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
225 {
226   return udp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */);
227 }
228
229 VLIB_REGISTER_NODE (udp4_output_node) =
230 {
231   .name = "udp4-output",
232   .vector_size = sizeof (u32),
233   .n_errors = UDP_N_ERROR,
234   .protocol_hint = VLIB_NODE_PROTO_HINT_UDP,
235   .error_counters = udp_output_error_counters,
236   .n_next_nodes = UDP_OUTPUT_N_NEXT,
237   .next_nodes = {
238 #define _(s, n) [UDP_OUTPUT_NEXT_##s] = n,
239     foreach_udp4_output_next
240 #undef _
241   },
242   .format_buffer = format_udp_header,
243   .format_trace = format_udp_tx_trace,
244 };
245
246 VLIB_REGISTER_NODE (udp6_output_node) =
247 {
248   .name = "udp6-output",
249   .vector_size = sizeof (u32),
250   .n_errors = UDP_N_ERROR,
251   .protocol_hint = VLIB_NODE_PROTO_HINT_UDP,
252   .error_counters = udp_output_error_counters,
253   .n_next_nodes = UDP_OUTPUT_N_NEXT,
254   .next_nodes = {
255 #define _(s, n) [UDP_OUTPUT_NEXT_##s] = n,
256     foreach_udp6_output_next
257 #undef _
258   },
259   .format_buffer = format_udp_header,
260   .format_trace = format_udp_tx_trace,
261 };
262
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */