session: pass tx buffers in bulk to transports
[vpp.git] / src / vnet / udp / udp.c
1 /*
2  * Copyright (c) 2016-2020 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/udp/udp.h>
17 #include <vnet/session/session.h>
18 #include <vnet/dpo/load_balance.h>
19 #include <vnet/ip/ip4_inlines.h>
20 #include <vnet/ip/ip6_inlines.h>
21 #include <vppinfra/sparse_vec.h>
22
23 udp_main_t udp_main;
24
25 static void
26 udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
27 {
28   udp_main_t *um = &udp_main;
29   udp_dst_port_info_t *pi;
30   u16 *n;
31
32   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
33   if (!pi)
34     {
35       udp_add_dst_port (um, lcl_port, 0, is_ip4);
36       pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
37       pi->n_connections = 1;
38     }
39   else
40     {
41       pi->n_connections += 1;
42       /* Do not return. The fact that the pi is valid does not mean
43        * it's up to date */
44     }
45
46   pi->node_index = is_ip4 ? udp4_input_node.index : udp6_input_node.index;
47   pi->next_index = um->local_to_input_edge[is_ip4];
48
49   /* Setup udp protocol -> next index sparse vector mapping. */
50   if (is_ip4)
51     n = sparse_vec_validate (um->next_by_dst_port4,
52                              clib_host_to_net_u16 (lcl_port));
53   else
54     n = sparse_vec_validate (um->next_by_dst_port6,
55                              clib_host_to_net_u16 (lcl_port));
56
57   n[0] = pi->next_index;
58 }
59
60 static void
61 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
62 {
63   udp_main_t *um = &udp_main;
64   udp_dst_port_info_t *pi;
65
66   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
67   if (!pi)
68     return;
69
70   if (!pi->n_connections)
71     {
72       clib_warning ("no connections using port %u", lcl_port);
73       return;
74     }
75
76   if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
77     udp_unregister_dst_port (0, lcl_port, is_ip4);
78 }
79
80 void
81 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
82 {
83   udp_main_t *um = &udp_main;
84   udp_dst_port_info_t *pi;
85
86   /* Done without a lock but the operation is atomic. Writers to pi hash
87    * table and vector should be guarded by a barrier sync */
88   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
89   clib_atomic_fetch_add_rel (&pi->n_connections, 1);
90 }
91
92 udp_connection_t *
93 udp_connection_alloc (u32 thread_index)
94 {
95   udp_main_t *um = &udp_main;
96   udp_connection_t *uc;
97   u32 will_expand = 0;
98   pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
99                                 CLIB_CACHE_LINE_BYTES);
100
101   if (PREDICT_FALSE (will_expand))
102     {
103       clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
104                                   [thread_index]);
105       pool_get_aligned (udp_main.connections[thread_index], uc,
106                         CLIB_CACHE_LINE_BYTES);
107       clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
108                                     [thread_index]);
109     }
110   else
111     {
112       pool_get_aligned (um->connections[thread_index], uc,
113                         CLIB_CACHE_LINE_BYTES);
114     }
115   clib_memset (uc, 0, sizeof (*uc));
116   uc->c_c_index = uc - um->connections[thread_index];
117   uc->c_thread_index = thread_index;
118   uc->c_proto = TRANSPORT_PROTO_UDP;
119   return uc;
120 }
121
122 void
123 udp_connection_free (udp_connection_t * uc)
124 {
125   u32 thread_index = uc->c_thread_index;
126   clib_spinlock_free (&uc->rx_lock);
127   if (CLIB_DEBUG)
128     clib_memset (uc, 0xFA, sizeof (*uc));
129   pool_put (udp_main.connections[thread_index], uc);
130 }
131
132 static void
133 udp_connection_cleanup (udp_connection_t * uc)
134 {
135   transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
136                               uc->c_lcl_port);
137   udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
138                                   uc->c_is_ip4);
139   udp_connection_free (uc);
140 }
141
142 void
143 udp_connection_delete (udp_connection_t * uc)
144 {
145   session_transport_delete_notify (&uc->connection);
146   udp_connection_cleanup (uc);
147 }
148
149 static u8
150 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
151 {
152   udp_main_t *um = vnet_get_udp_main ();
153   udp_dst_port_info_t *pi;
154
155   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
156   return (pi && !pi->n_connections
157           && udp_is_valid_dst_port (lcl_port, is_ip4));
158 }
159
160 static u16
161 udp_default_mtu (udp_main_t * um, u8 is_ip4)
162 {
163   u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
164   return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
165 }
166
167 static u32
168 udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
169 {
170   udp_main_t *um = vnet_get_udp_main ();
171   vlib_main_t *vm = vlib_get_main ();
172   transport_endpoint_cfg_t *lcl_ext;
173   udp_connection_t *listener;
174   u16 lcl_port_ho;
175   void *iface_ip;
176
177   lcl_port_ho = clib_net_to_host_u16 (lcl->port);
178
179   if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
180     {
181       clib_warning ("port already used");
182       return SESSION_E_PORTINUSE;
183     }
184
185   pool_get (um->listener_pool, listener);
186   clib_memset (listener, 0, sizeof (udp_connection_t));
187
188   listener->c_lcl_port = lcl->port;
189   listener->c_c_index = listener - um->listener_pool;
190
191   /* If we are provided a sw_if_index, bind using one of its ips */
192   if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
193     {
194       if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
195                                                  lcl->is_ip4)))
196         ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
197     }
198   ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
199   listener->c_is_ip4 = lcl->is_ip4;
200   listener->c_proto = TRANSPORT_PROTO_UDP;
201   listener->c_s_index = session_index;
202   listener->c_fib_index = lcl->fib_index;
203   listener->mss = udp_default_mtu (um, listener->c_is_ip4);
204   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
205   lcl_ext = (transport_endpoint_cfg_t *) lcl;
206   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
207     listener->flags |= UDP_CONN_F_CONNECTED;
208   else
209     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
210   clib_spinlock_init (&listener->rx_lock);
211
212   udp_connection_register_port (vm, lcl_port_ho, lcl->is_ip4);
213   return listener->c_c_index;
214 }
215
216 static u32
217 udp_session_unbind (u32 listener_index)
218 {
219   udp_main_t *um = &udp_main;
220   udp_connection_t *listener;
221
222   listener = udp_listener_get (listener_index);
223   udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
224                                   listener->c_is_ip4);
225   clib_spinlock_free (&listener->rx_lock);
226   pool_put (um->listener_pool, listener);
227   return 0;
228 }
229
230 static transport_connection_t *
231 udp_session_get_listener (u32 listener_index)
232 {
233   udp_connection_t *us;
234
235   us = udp_listener_get (listener_index);
236   return &us->connection;
237 }
238
239 always_inline u32
240 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
241 {
242   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
243   if (uc->c_is_ip4)
244     vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
245                                  IP_PROTOCOL_UDP, 1 /* csum offload */,
246                                  0 /* is_df */, uc->c_dscp);
247   else
248     vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
249                           IP_PROTOCOL_UDP);
250   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
251   vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
252   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
253
254   return 0;
255 }
256
257 static u32
258 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
259 {
260   vlib_main_t *vm = vlib_get_main ();
261   udp_connection_t *uc;
262
263   uc = udp_connection_from_transport (tc);
264
265   while (n_bufs >= 4)
266     {
267       vlib_prefetch_buffer_header (bs[2], STORE);
268       vlib_prefetch_buffer_header (bs[3], STORE);
269
270       udp_push_one_header (vm, uc, bs[0]);
271       udp_push_one_header (vm, uc, bs[1]);
272
273       n_bufs -= 2;
274       bs += 2;
275     }
276   while (n_bufs)
277     {
278       if (n_bufs > 1)
279         vlib_prefetch_buffer_header (bs[1], STORE);
280
281       udp_push_one_header (vm, uc, bs[0]);
282
283       n_bufs -= 1;
284       bs += 1;
285     }
286
287   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
288     {
289       if (!transport_max_tx_dequeue (&uc->connection))
290         udp_connection_delete (uc);
291     }
292
293   return 0;
294 }
295
296 static transport_connection_t *
297 udp_session_get (u32 connection_index, u32 thread_index)
298 {
299   udp_connection_t *uc;
300   uc = udp_connection_get (connection_index, thread_index);
301   if (uc)
302     return &uc->connection;
303   return 0;
304 }
305
306 static void
307 udp_session_close (u32 connection_index, u32 thread_index)
308 {
309   udp_connection_t *uc;
310
311   uc = udp_connection_get (connection_index, thread_index);
312   if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
313     return;
314
315   if (!transport_max_tx_dequeue (&uc->connection))
316     udp_connection_delete (uc);
317   else
318     uc->flags |= UDP_CONN_F_CLOSING;
319 }
320
321 static void
322 udp_session_cleanup (u32 connection_index, u32 thread_index)
323 {
324   udp_connection_t *uc;
325   uc = udp_connection_get (connection_index, thread_index);
326   if (!uc)
327     return;
328   if (uc->flags & UDP_CONN_F_MIGRATED)
329     udp_connection_free (uc);
330   else
331     udp_connection_cleanup (uc);
332 }
333
334 static int
335 udp_session_send_params (transport_connection_t * tconn,
336                          transport_send_params_t * sp)
337 {
338   udp_connection_t *uc;
339
340   uc = udp_connection_from_transport (tconn);
341
342   /* No constraint on TX window */
343   sp->snd_space = ~0;
344   /* TODO figure out MTU of output interface */
345   sp->snd_mss = uc->mss;
346   sp->tx_offset = 0;
347   sp->flags = 0;
348   return 0;
349 }
350
351 static int
352 udp_open_connection (transport_endpoint_cfg_t * rmt)
353 {
354   vlib_main_t *vm = vlib_get_main ();
355   u32 thread_index = vm->thread_index;
356   udp_main_t *um = &udp_main;
357   ip46_address_t lcl_addr;
358   udp_connection_t *uc;
359   u16 lcl_port;
360   int rv;
361
362   rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
363                                        &lcl_port);
364   if (rv)
365     {
366       if (rv != SESSION_E_PORTINUSE)
367         return rv;
368
369       if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
370         return SESSION_E_PORTINUSE;
371
372       /* If port in use, check if 5-tuple is also in use */
373       if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
374                                      lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
375                                      rmt->is_ip4))
376         return SESSION_E_PORTINUSE;
377
378       /* 5-tuple is available so increase lcl endpoint refcount and proceed
379        * with connection allocation */
380       transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
381                                       lcl_port);
382       goto conn_alloc;
383     }
384
385   if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
386     {
387       /* If specific source port was requested abort */
388       if (rmt->peer.port)
389         return SESSION_E_PORTINUSE;
390
391       /* Try to find a port that's not used */
392       while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
393         {
394           lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
395                                                  &lcl_addr);
396           if (lcl_port < 1)
397             return SESSION_E_PORTINUSE;
398         }
399     }
400
401 conn_alloc:
402
403   udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
404
405   /* We don't poll main thread if we have workers */
406   thread_index = transport_cl_thread ();
407
408   uc = udp_connection_alloc (thread_index);
409   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
410   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
411   uc->c_rmt_port = rmt->port;
412   uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
413   uc->c_is_ip4 = rmt->is_ip4;
414   uc->c_proto = TRANSPORT_PROTO_UDP;
415   uc->c_fib_index = rmt->fib_index;
416   uc->c_dscp = rmt->dscp;
417   uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
418   uc->flags |= UDP_CONN_F_OWNS_PORT;
419   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
420     {
421       uc->flags |= UDP_CONN_F_CONNECTED;
422     }
423   else
424     {
425       clib_spinlock_init (&uc->rx_lock);
426       uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
427     }
428
429   return uc->c_c_index;
430 }
431
432 static transport_connection_t *
433 udp_session_get_half_open (u32 conn_index)
434 {
435   udp_connection_t *uc;
436   u32 thread_index;
437
438   /* We don't poll main thread if we have workers */
439   thread_index = transport_cl_thread ();
440   uc = udp_connection_get (conn_index, thread_index);
441   if (!uc)
442     return 0;
443   return &uc->connection;
444 }
445
446 static u8 *
447 format_udp_session (u8 * s, va_list * args)
448 {
449   u32 uci = va_arg (*args, u32);
450   u32 thread_index = va_arg (*args, u32);
451   u32 verbose = va_arg (*args, u32);
452   udp_connection_t *uc;
453
454   uc = udp_connection_get (uci, thread_index);
455   return format (s, "%U", format_udp_connection, uc, verbose);
456 }
457
458 static u8 *
459 format_udp_half_open_session (u8 * s, va_list * args)
460 {
461   u32 __clib_unused tci = va_arg (*args, u32);
462   u32 __clib_unused thread_index = va_arg (*args, u32);
463   clib_warning ("BUG");
464   return 0;
465 }
466
467 static u8 *
468 format_udp_listener_session (u8 * s, va_list * args)
469 {
470   u32 tci = va_arg (*args, u32);
471   u32 __clib_unused thread_index = va_arg (*args, u32);
472   u32 verbose = va_arg (*args, u32);
473   udp_connection_t *uc = udp_listener_get (tci);
474   return format (s, "%U", format_udp_connection, uc, verbose);
475 }
476
477 /* *INDENT-OFF* */
478 static const transport_proto_vft_t udp_proto = {
479   .start_listen = udp_session_bind,
480   .connect = udp_open_connection,
481   .stop_listen = udp_session_unbind,
482   .push_header = udp_push_header,
483   .get_connection = udp_session_get,
484   .get_listener = udp_session_get_listener,
485   .get_half_open = udp_session_get_half_open,
486   .close = udp_session_close,
487   .cleanup = udp_session_cleanup,
488   .send_params = udp_session_send_params,
489   .format_connection = format_udp_session,
490   .format_half_open = format_udp_half_open_session,
491   .format_listener = format_udp_listener_session,
492   .transport_options = {
493     .name = "udp",
494     .short_name = "U",
495     .tx_type = TRANSPORT_TX_DGRAM,
496     .service_type = TRANSPORT_SERVICE_CL,
497   },
498 };
499 /* *INDENT-ON* */
500
501 static clib_error_t *
502 udp_init (vlib_main_t * vm)
503 {
504   udp_main_t *um = vnet_get_udp_main ();
505   ip_main_t *im = &ip_main;
506   vlib_thread_main_t *tm = vlib_get_thread_main ();
507   u32 num_threads;
508   ip_protocol_info_t *pi;
509   int i;
510
511   /*
512    * Registrations
513    */
514
515   /* IP registration */
516   pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
517   if (pi == 0)
518     return clib_error_return (0, "UDP protocol info AWOL");
519   pi->format_header = format_udp_header;
520   pi->unformat_pg_edit = unformat_pg_udp_header;
521
522   /* Register as transport with URI */
523   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
524                                FIB_PROTOCOL_IP4, ip4_lookup_node.index);
525   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
526                                FIB_PROTOCOL_IP6, ip6_lookup_node.index);
527
528   /*
529    * Initialize data structures
530    */
531
532   num_threads = 1 /* main thread */  + tm->n_threads;
533   vec_validate (um->connections, num_threads - 1);
534   vec_validate (um->connection_peekers, num_threads - 1);
535   vec_validate (um->peekers_readers_locks, num_threads - 1);
536   vec_validate (um->peekers_write_locks, num_threads - 1);
537
538   if (num_threads > 1)
539     for (i = 0; i < num_threads; i++)
540       {
541         clib_spinlock_init (&um->peekers_readers_locks[i]);
542         clib_spinlock_init (&um->peekers_write_locks[i]);
543       }
544
545   um->local_to_input_edge[UDP_IP4] =
546     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
547   um->local_to_input_edge[UDP_IP6] =
548     vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
549
550   um->default_mtu = 1500;
551   return 0;
552 }
553
554 /* *INDENT-OFF* */
555 VLIB_INIT_FUNCTION (udp_init) =
556 {
557   .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
558                            "ip6_lookup_init"),
559 };
560 /* *INDENT-ON* */
561
562 /*
563  * fd.io coding-style-patch-verification: ON
564  *
565  * Local Variables:
566  * eval: (c-set-style "gnu")
567  * End:
568  */