]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/toecore.c
Merge ACPICA 20191018.
[FreeBSD/FreeBSD.git] / sys / netinet / toecore.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35
36 #include <sys/param.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/module.h>
43 #include <sys/types.h>
44 #include <sys/sockopt.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47
48 #include <net/ethernet.h>
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/if_vlan_var.h>
53 #include <net/if_llatbl.h>
54 #include <net/route.h>
55
56 #include <netinet/if_ether.h>
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/in6_pcb.h>
62 #include <netinet6/nd6.h>
63 #define TCPSTATES
64 #include <netinet/tcp.h>
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcp_syncache.h>
69 #include <netinet/tcp_offload.h>
70 #include <netinet/toecore.h>
71
72 static struct mtx toedev_lock;
73 static TAILQ_HEAD(, toedev) toedev_list;
74 static eventhandler_tag listen_start_eh;
75 static eventhandler_tag listen_stop_eh;
76 static eventhandler_tag lle_event_eh;
77
78 static int
79 toedev_connect(struct toedev *tod __unused, struct socket *so __unused,
80     struct rtentry *rt __unused, struct sockaddr *nam __unused)
81 {
82
83         return (ENOTSUP);
84 }
85
86 static int
87 toedev_listen_start(struct toedev *tod __unused, struct tcpcb *tp __unused)
88 {
89
90         return (ENOTSUP);
91 }
92
93 static int
94 toedev_listen_stop(struct toedev *tod __unused, struct tcpcb *tp __unused)
95 {
96
97         return (ENOTSUP);
98 }
99
100 static void
101 toedev_input(struct toedev *tod __unused, struct tcpcb *tp __unused,
102     struct mbuf *m)
103 {
104
105         m_freem(m);
106         return;
107 }
108
109 static void
110 toedev_rcvd(struct toedev *tod __unused, struct tcpcb *tp __unused)
111 {
112
113         return;
114 }
115
116 static int
117 toedev_output(struct toedev *tod __unused, struct tcpcb *tp __unused)
118 {
119
120         return (ENOTSUP);
121 }
122
123 static void
124 toedev_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp __unused)
125 {
126
127         return;
128 }
129
130 static void
131 toedev_l2_update(struct toedev *tod __unused, struct ifnet *ifp __unused,
132     struct sockaddr *sa __unused, uint8_t *lladdr __unused,
133     uint16_t vtag __unused)
134 {
135
136         return;
137 }
138
139 static void
140 toedev_route_redirect(struct toedev *tod __unused, struct ifnet *ifp __unused,
141     struct rtentry *rt0 __unused, struct rtentry *rt1 __unused)
142 {
143
144         return;
145 }
146
147 static void
148 toedev_syncache_added(struct toedev *tod __unused, void *ctx __unused)
149 {
150
151         return;
152 }
153
154 static void
155 toedev_syncache_removed(struct toedev *tod __unused, void *ctx __unused)
156 {
157
158         return;
159 }
160
161 static int
162 toedev_syncache_respond(struct toedev *tod __unused, void *ctx __unused,
163     struct mbuf *m)
164 {
165
166         m_freem(m);
167         return (0);
168 }
169
170 static void
171 toedev_offload_socket(struct toedev *tod __unused, void *ctx __unused,
172     struct socket *so __unused)
173 {
174
175         return;
176 }
177
178 static void
179 toedev_ctloutput(struct toedev *tod __unused, struct tcpcb *tp __unused,
180     int sopt_dir __unused, int sopt_name __unused)
181 {
182
183         return;
184 }
185
186 static void
187 toedev_tcp_info(struct toedev *tod __unused, struct tcpcb *tp __unused,
188     struct tcp_info *ti __unused)
189 {
190
191         return;
192 }
193
194 static int
195 toedev_alloc_tls_session(struct toedev *tod __unused, struct tcpcb *tp __unused,
196     struct ktls_session *tls __unused)
197 {
198
199         return (EINVAL);
200 }
201
202 /*
203  * Inform one or more TOE devices about a listening socket.
204  */
205 static void
206 toe_listen_start(struct inpcb *inp, void *arg)
207 {
208         struct toedev *t, *tod;
209         struct tcpcb *tp;
210
211         INP_WLOCK_ASSERT(inp);
212         KASSERT(inp->inp_pcbinfo == &V_tcbinfo,
213             ("%s: inp is not a TCP inp", __func__));
214
215         if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))
216                 return;
217
218         tp = intotcpcb(inp);
219         if (tp->t_state != TCPS_LISTEN)
220                 return;
221
222         t = arg;
223         mtx_lock(&toedev_lock);
224         TAILQ_FOREACH(tod, &toedev_list, link) {
225                 if (t == NULL || t == tod)
226                         tod->tod_listen_start(tod, tp);
227         }
228         mtx_unlock(&toedev_lock);
229 }
230
231 static void
232 toe_listen_start_event(void *arg __unused, struct tcpcb *tp)
233 {
234         struct inpcb *inp = tp->t_inpcb;
235
236         INP_WLOCK_ASSERT(inp);
237         KASSERT(tp->t_state == TCPS_LISTEN,
238             ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
239
240         toe_listen_start(inp, NULL);
241 }
242
243 static void
244 toe_listen_stop_event(void *arg __unused, struct tcpcb *tp)
245 {
246         struct toedev *tod;
247 #ifdef INVARIANTS
248         struct inpcb *inp = tp->t_inpcb;
249 #endif
250
251         INP_WLOCK_ASSERT(inp);
252         KASSERT(tp->t_state == TCPS_LISTEN,
253             ("%s: t_state %s", __func__, tcpstates[tp->t_state]));
254
255         mtx_lock(&toedev_lock);
256         TAILQ_FOREACH(tod, &toedev_list, link)
257             tod->tod_listen_stop(tod, tp);
258         mtx_unlock(&toedev_lock);
259 }
260
261 /*
262  * Fill up a freshly allocated toedev struct with reasonable defaults.
263  */
264 void
265 init_toedev(struct toedev *tod)
266 {
267
268         tod->tod_softc = NULL;
269
270         /*
271          * Provide no-op defaults so that the kernel can call any toedev
272          * function without having to check whether the TOE driver supplied one
273          * or not.
274          */
275         tod->tod_connect = toedev_connect;
276         tod->tod_listen_start = toedev_listen_start;
277         tod->tod_listen_stop = toedev_listen_stop;
278         tod->tod_input = toedev_input;
279         tod->tod_rcvd = toedev_rcvd;
280         tod->tod_output = toedev_output;
281         tod->tod_send_rst = toedev_output;
282         tod->tod_send_fin = toedev_output;
283         tod->tod_pcb_detach = toedev_pcb_detach;
284         tod->tod_l2_update = toedev_l2_update;
285         tod->tod_route_redirect = toedev_route_redirect;
286         tod->tod_syncache_added = toedev_syncache_added;
287         tod->tod_syncache_removed = toedev_syncache_removed;
288         tod->tod_syncache_respond = toedev_syncache_respond;
289         tod->tod_offload_socket = toedev_offload_socket;
290         tod->tod_ctloutput = toedev_ctloutput;
291         tod->tod_tcp_info = toedev_tcp_info;
292         tod->tod_alloc_tls_session = toedev_alloc_tls_session;
293 }
294
295 /*
296  * Register an active TOE device with the system.  This allows it to receive
297  * notifications from the kernel.
298  */
299 int
300 register_toedev(struct toedev *tod)
301 {
302         struct toedev *t;
303
304         mtx_lock(&toedev_lock);
305         TAILQ_FOREACH(t, &toedev_list, link) {
306                 if (t == tod) {
307                         mtx_unlock(&toedev_lock);
308                         return (EEXIST);
309                 }
310         }
311
312         TAILQ_INSERT_TAIL(&toedev_list, tod, link);
313         registered_toedevs++;
314         mtx_unlock(&toedev_lock);
315
316         inp_apply_all(toe_listen_start, tod);
317
318         return (0);
319 }
320
321 /*
322  * Remove the TOE device from the global list of active TOE devices.  It is the
323  * caller's responsibility to ensure that the TOE device is quiesced prior to
324  * this call.
325  */
326 int
327 unregister_toedev(struct toedev *tod)
328 {
329         struct toedev *t, *t2;
330         int rc = ENODEV;
331
332         mtx_lock(&toedev_lock);
333         TAILQ_FOREACH_SAFE(t, &toedev_list, link, t2) {
334                 if (t == tod) {
335                         TAILQ_REMOVE(&toedev_list, tod, link);
336                         registered_toedevs--;
337                         rc = 0;
338                         break;
339                 }
340         }
341         KASSERT(registered_toedevs >= 0,
342             ("%s: registered_toedevs (%d) < 0", __func__, registered_toedevs));
343         mtx_unlock(&toedev_lock);
344         return (rc);
345 }
346
347 void
348 toe_syncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
349     struct inpcb *inp, void *tod, void *todctx)
350 {
351         struct socket *lso = inp->inp_socket;
352
353         INP_WLOCK_ASSERT(inp);
354
355         syncache_add(inc, to, th, inp, &lso, NULL, tod, todctx);
356 }
357
358 int
359 toe_syncache_expand(struct in_conninfo *inc, struct tcpopt *to,
360     struct tcphdr *th, struct socket **lsop)
361 {
362
363         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
364
365         return (syncache_expand(inc, to, th, lsop, NULL));
366 }
367
368 /*
369  * General purpose check to see if a 4-tuple is in use by the kernel.  If a TCP
370  * header (presumably for an incoming SYN) is also provided, an existing 4-tuple
371  * in TIME_WAIT may be assassinated freeing it up for re-use.
372  *
373  * Note that the TCP header must have been run through tcp_fields_to_host() or
374  * equivalent.
375  */
376 int
377 toe_4tuple_check(struct in_conninfo *inc, struct tcphdr *th, struct ifnet *ifp)
378 {
379         struct inpcb *inp;
380
381         if (inc->inc_flags & INC_ISIPV6) {
382                 inp = in6_pcblookup(&V_tcbinfo, &inc->inc6_faddr,
383                     inc->inc_fport, &inc->inc6_laddr, inc->inc_lport,
384                     INPLOOKUP_WLOCKPCB, ifp);
385         } else {
386                 inp = in_pcblookup(&V_tcbinfo, inc->inc_faddr, inc->inc_fport,
387                     inc->inc_laddr, inc->inc_lport, INPLOOKUP_WLOCKPCB, ifp);
388         }
389         if (inp != NULL) {
390                 INP_WLOCK_ASSERT(inp);
391
392                 if ((inp->inp_flags & INP_TIMEWAIT) && th != NULL) {
393
394                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo); /* for twcheck */
395                         if (!tcp_twcheck(inp, NULL, th, NULL, 0))
396                                 return (EADDRINUSE);
397                 } else {
398                         INP_WUNLOCK(inp);
399                         return (EADDRINUSE);
400                 }
401         }
402
403         return (0);
404 }
405
406 static void
407 toe_lle_event(void *arg __unused, struct llentry *lle, int evt)
408 {
409         struct toedev *tod;
410         struct ifnet *ifp;
411         struct sockaddr *sa;
412         uint8_t *lladdr;
413         uint16_t vid, pcp;
414         int family;
415         struct sockaddr_in6 sin6;
416
417         LLE_WLOCK_ASSERT(lle);
418
419         ifp = lltable_get_ifp(lle->lle_tbl);
420         family = lltable_get_af(lle->lle_tbl);
421
422         if (family != AF_INET && family != AF_INET6)
423                 return;
424         /*
425          * Not interested if the interface's TOE capability is not enabled.
426          */
427         if ((family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) ||
428             (family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)))
429                 return;
430
431         tod = TOEDEV(ifp);
432         if (tod == NULL)
433                 return;
434
435         sa = (struct sockaddr *)&sin6;
436         lltable_fill_sa_entry(lle, sa);
437
438         vid = 0xfff;
439         pcp = 0;
440         if (evt != LLENTRY_RESOLVED) {
441
442                 /*
443                  * LLENTRY_TIMEDOUT, LLENTRY_DELETED, LLENTRY_EXPIRED all mean
444                  * this entry is going to be deleted.
445                  */
446
447                 lladdr = NULL;
448         } else {
449
450                 KASSERT(lle->la_flags & LLE_VALID,
451                     ("%s: %p resolved but not valid?", __func__, lle));
452
453                 lladdr = (uint8_t *)lle->ll_addr;
454                 VLAN_TAG(ifp, &vid);
455                 VLAN_PCP(ifp, &pcp);
456         }
457
458         tod->tod_l2_update(tod, ifp, sa, lladdr, EVL_MAKETAG(vid, pcp, 0));
459 }
460
461 /*
462  * Returns 0 or EWOULDBLOCK on success (any other value is an error).  0 means
463  * lladdr and vtag are valid on return, EWOULDBLOCK means the TOE driver's
464  * tod_l2_update will be called later, when the entry is resolved or times out.
465  */
466 int
467 toe_l2_resolve(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
468     uint8_t *lladdr, uint16_t *vtag)
469 {
470         int rc;
471         uint16_t vid, pcp;
472
473         switch (sa->sa_family) {
474 #ifdef INET
475         case AF_INET:
476                 rc = arpresolve(ifp, 0, NULL, sa, lladdr, NULL, NULL);
477                 break;
478 #endif
479 #ifdef INET6
480         case AF_INET6:
481                 rc = nd6_resolve(ifp, 0, NULL, sa, lladdr, NULL, NULL);
482                 break;
483 #endif
484         default:
485                 return (EPROTONOSUPPORT);
486         }
487
488         if (rc == 0) {
489                 vid = 0xfff;
490                 pcp = 0;
491                 if (ifp->if_type == IFT_L2VLAN) {
492                         VLAN_TAG(ifp, &vid);
493                         VLAN_PCP(ifp, &pcp);
494                 } else if (ifp->if_pcp != IFNET_PCP_NONE) {
495                         vid = 0;
496                         pcp = ifp->if_pcp;
497                 }
498                 *vtag = EVL_MAKETAG(vid, pcp, 0);
499         }
500
501         return (rc);
502 }
503
504 void
505 toe_connect_failed(struct toedev *tod, struct inpcb *inp, int err)
506 {
507
508         INP_WLOCK_ASSERT(inp);
509
510         if (!(inp->inp_flags & INP_DROPPED)) {
511                 struct tcpcb *tp = intotcpcb(inp);
512
513                 KASSERT(tp->t_flags & TF_TOE,
514                     ("%s: tp %p not offloaded.", __func__, tp));
515
516                 if (err == EAGAIN) {
517
518                         /*
519                          * Temporary failure during offload, take this PCB back.
520                          * Detach from the TOE driver and do the rest of what
521                          * TCP's pru_connect would have done if the connection
522                          * wasn't offloaded.
523                          */
524
525                         tod->tod_pcb_detach(tod, tp);
526                         KASSERT(!(tp->t_flags & TF_TOE),
527                             ("%s: tp %p still offloaded.", __func__, tp));
528                         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
529                         (void) tp->t_fb->tfb_tcp_output(tp);
530                 } else {
531
532                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
533                         tp = tcp_drop(tp, err);
534                         if (tp == NULL)
535                                 INP_WLOCK(inp); /* re-acquire */
536                 }
537         }
538         INP_WLOCK_ASSERT(inp);
539 }
540
541 static int
542 toecore_load(void)
543 {
544
545         mtx_init(&toedev_lock, "toedev lock", NULL, MTX_DEF);
546         TAILQ_INIT(&toedev_list);
547
548         listen_start_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_start,
549             toe_listen_start_event, NULL, EVENTHANDLER_PRI_ANY);
550         listen_stop_eh = EVENTHANDLER_REGISTER(tcp_offload_listen_stop,
551             toe_listen_stop_event, NULL, EVENTHANDLER_PRI_ANY);
552         lle_event_eh = EVENTHANDLER_REGISTER(lle_event, toe_lle_event, NULL,
553             EVENTHANDLER_PRI_ANY);
554
555         return (0);
556 }
557
558 static int
559 toecore_unload(void)
560 {
561
562         mtx_lock(&toedev_lock);
563         if (!TAILQ_EMPTY(&toedev_list)) {
564                 mtx_unlock(&toedev_lock);
565                 return (EBUSY);
566         }
567
568         EVENTHANDLER_DEREGISTER(tcp_offload_listen_start, listen_start_eh);
569         EVENTHANDLER_DEREGISTER(tcp_offload_listen_stop, listen_stop_eh);
570         EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
571
572         mtx_unlock(&toedev_lock);
573         mtx_destroy(&toedev_lock);
574
575         return (0);
576 }
577
578 static int
579 toecore_mod_handler(module_t mod, int cmd, void *arg)
580 {
581
582         if (cmd == MOD_LOAD)
583                 return (toecore_load());
584
585         if (cmd == MOD_UNLOAD)
586                 return (toecore_unload());
587
588         return (EOPNOTSUPP);
589 }
590
591 static moduledata_t mod_data= {
592         "toecore",
593         toecore_mod_handler,
594         0
595 };
596
597 MODULE_VERSION(toecore, 1);
598 DECLARE_MODULE(toecore, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);