Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/net/if_epair.c
104875 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2008 The FreeBSD Foundation
5
* Copyright (c) 2009-2021 Bjoern A. Zeeb <[email protected]>
6
*
7
* This software was developed by CK Software GmbH under sponsorship
8
* from the FreeBSD Foundation.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
* SUCH DAMAGE.
30
*/
31
32
/*
33
* A pair of virtual back-to-back connected ethernet like interfaces
34
* (``two interfaces with a virtual cross-over cable'').
35
*
36
* This is mostly intended to be used to provide connectivity between
37
* different virtual network stack instances.
38
*/
39
40
#include <sys/cdefs.h>
41
#include "opt_rss.h"
42
#include "opt_inet.h"
43
#include "opt_inet6.h"
44
45
#include <sys/param.h>
46
#include <sys/bus.h>
47
#include <sys/hash.h>
48
#include <sys/interrupt.h>
49
#include <sys/jail.h>
50
#include <sys/kernel.h>
51
#include <sys/libkern.h>
52
#include <sys/malloc.h>
53
#include <sys/mbuf.h>
54
#include <sys/module.h>
55
#include <sys/proc.h>
56
#include <sys/queue.h>
57
#include <sys/sched.h>
58
#include <sys/smp.h>
59
#include <sys/socket.h>
60
#include <sys/sockio.h>
61
#include <sys/sysctl.h>
62
#include <sys/taskqueue.h>
63
64
#include <net/bpf.h>
65
#include <net/ethernet.h>
66
#include <net/if.h>
67
#include <net/if_var.h>
68
#include <net/if_clone.h>
69
#include <net/if_media.h>
70
#include <net/if_private.h>
71
#include <net/if_types.h>
72
#include <net/if_vlan_var.h>
73
#include <net/netisr.h>
74
#ifdef RSS
75
#include <net/rss_config.h>
76
#ifdef INET
77
#include <netinet/in_rss.h>
78
#endif
79
#ifdef INET6
80
#include <netinet6/in6_rss.h>
81
#endif
82
#endif
83
#include <net/vnet.h>
84
85
static const char epairname[] = "epair";
86
#define RXRSIZE 4096 /* Probably overkill by 4-8x. */
87
88
static MALLOC_DEFINE(M_EPAIR, epairname,
89
"Pair of virtual cross-over connected Ethernet-like interfaces");
90
91
VNET_DEFINE_STATIC(struct if_clone *, epair_cloner);
92
#define V_epair_cloner VNET(epair_cloner)
93
94
static unsigned int next_index = 0;
95
#define EPAIR_LOCK_INIT() mtx_init(&epair_n_index_mtx, "epairidx", \
96
NULL, MTX_DEF)
97
#define EPAIR_LOCK_DESTROY() mtx_destroy(&epair_n_index_mtx)
98
#define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx)
99
#define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx)
100
101
SYSCTL_DECL(_net_link);
102
static SYSCTL_NODE(_net_link, OID_AUTO, epair, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
103
"Pair of virtual cross-over connected Ethernet-like interfaces");
104
105
static bool use_ether_gen_addr = true;
106
SYSCTL_BOOL(_net_link_epair, OID_AUTO, ether_gen_addr, CTLFLAG_RWTUN,
107
&use_ether_gen_addr, false,
108
"Generate MAC with FreeBSD OUI using ether_gen_addr(9)");
109
110
struct epair_softc;
111
struct epair_queue {
112
struct mtx mtx;
113
struct mbufq q;
114
int id;
115
enum {
116
EPAIR_QUEUE_IDLE,
117
EPAIR_QUEUE_WAKING,
118
EPAIR_QUEUE_RUNNING,
119
} state;
120
struct task tx_task;
121
struct epair_softc *sc;
122
};
123
124
static struct mtx epair_n_index_mtx;
125
struct epair_softc {
126
struct ifnet *ifp; /* This ifp. */
127
struct ifnet *oifp; /* other ifp of pair. */
128
int num_queues;
129
struct epair_queue *queues;
130
struct ifmedia media; /* Media config (fake). */
131
STAILQ_ENTRY(epair_softc) entry;
132
};
133
134
struct epair_tasks_t {
135
int tasks;
136
struct taskqueue *tq[MAXCPU];
137
};
138
139
static struct epair_tasks_t epair_tasks;
140
141
static void
142
epair_clear_mbuf(struct mbuf *m)
143
{
144
M_ASSERTPKTHDR(m);
145
146
/* Remove any CSUM_SND_TAG as ether_input will barf. */
147
if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
148
m_snd_tag_rele(m->m_pkthdr.snd_tag);
149
m->m_pkthdr.snd_tag = NULL;
150
m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
151
}
152
153
m_tag_delete_nonpersistent(m);
154
}
155
156
static void
157
epair_tx_start_deferred(void *arg, int pending)
158
{
159
struct epair_queue *q = (struct epair_queue *)arg;
160
if_t ifp;
161
struct mbuf *m, *n;
162
bool resched;
163
164
ifp = q->sc->ifp;
165
166
if_ref(ifp);
167
CURVNET_SET(ifp->if_vnet);
168
169
mtx_lock(&q->mtx);
170
m = mbufq_flush(&q->q);
171
q->state = EPAIR_QUEUE_RUNNING;
172
mtx_unlock(&q->mtx);
173
174
while (m != NULL) {
175
n = STAILQ_NEXT(m, m_stailqpkt);
176
m->m_nextpkt = NULL;
177
if_input(ifp, m);
178
m = n;
179
}
180
181
/*
182
* Avoid flushing the queue more than once per task. We can otherwise
183
* end up starving ourselves in a multi-epair routing configuration.
184
*/
185
mtx_lock(&q->mtx);
186
if (!mbufq_empty(&q->q)) {
187
resched = true;
188
q->state = EPAIR_QUEUE_WAKING;
189
} else {
190
resched = false;
191
q->state = EPAIR_QUEUE_IDLE;
192
}
193
mtx_unlock(&q->mtx);
194
195
if (resched)
196
taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
197
198
CURVNET_RESTORE();
199
if_rele(ifp);
200
}
201
202
static struct epair_queue *
203
epair_select_queue(struct epair_softc *sc, struct mbuf *m)
204
{
205
uint32_t bucket;
206
#ifdef RSS
207
struct ether_header *eh;
208
int ret;
209
210
ret = rss_m2bucket(m, &bucket);
211
if (ret) {
212
/* Actually hash the packet. */
213
eh = mtod(m, struct ether_header *);
214
215
switch (ntohs(eh->ether_type)) {
216
#ifdef INET
217
case ETHERTYPE_IP:
218
rss_soft_m2cpuid_v4(m, 0, &bucket);
219
break;
220
#endif
221
#ifdef INET6
222
case ETHERTYPE_IPV6:
223
rss_soft_m2cpuid_v6(m, 0, &bucket);
224
break;
225
#endif
226
default:
227
bucket = 0;
228
break;
229
}
230
}
231
bucket %= sc->num_queues;
232
#else
233
bucket = 0;
234
#endif
235
return (&sc->queues[bucket]);
236
}
237
238
static void
239
epair_prepare_mbuf(struct mbuf *m, struct ifnet *src_ifp)
240
{
241
M_ASSERTPKTHDR(m);
242
epair_clear_mbuf(m);
243
if_setrcvif(m, src_ifp);
244
M_SETFIB(m, src_ifp->if_fib);
245
246
MPASS(m->m_nextpkt == NULL);
247
MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
248
}
249
250
static void
251
epair_menq(struct mbuf *m, struct epair_softc *osc)
252
{
253
struct epair_queue *q;
254
struct ifnet *ifp, *oifp;
255
int error, len;
256
bool mcast;
257
258
/*
259
* I know this looks weird. We pass the "other sc" as we need that one
260
* and can get both ifps from it as well.
261
*/
262
oifp = osc->ifp;
263
ifp = osc->oifp;
264
265
epair_prepare_mbuf(m, oifp);
266
267
/* Save values as once the mbuf is queued, it's not ours anymore. */
268
len = m->m_pkthdr.len;
269
mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
270
271
q = epair_select_queue(osc, m);
272
273
mtx_lock(&q->mtx);
274
if (q->state == EPAIR_QUEUE_IDLE) {
275
q->state = EPAIR_QUEUE_WAKING;
276
taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task);
277
}
278
error = mbufq_enqueue(&q->q, m);
279
mtx_unlock(&q->mtx);
280
281
if (error != 0) {
282
m_freem(m);
283
if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
284
} else {
285
if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
286
if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
287
if (mcast)
288
if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
289
if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1);
290
}
291
}
292
293
static void
294
epair_start(struct ifnet *ifp)
295
{
296
struct mbuf *m;
297
struct epair_softc *sc;
298
struct ifnet *oifp;
299
300
/*
301
* We get packets here from ether_output via if_handoff()
302
* and need to put them into the input queue of the oifp
303
* and will put the packet into the receive-queue (rxq) of the
304
* other interface (oifp) of our pair.
305
*/
306
sc = ifp->if_softc;
307
oifp = sc->oifp;
308
sc = oifp->if_softc;
309
for (;;) {
310
IFQ_DEQUEUE(&ifp->if_snd, m);
311
if (m == NULL)
312
break;
313
M_ASSERTPKTHDR(m);
314
BPF_MTAP(ifp, m);
315
316
/* In case either interface is not usable drop the packet. */
317
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
318
(ifp->if_flags & IFF_UP) == 0 ||
319
(oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
320
(oifp->if_flags & IFF_UP) == 0) {
321
m_freem(m);
322
continue;
323
}
324
325
epair_menq(m, sc);
326
}
327
}
328
329
static int
330
epair_transmit(struct ifnet *ifp, struct mbuf *m)
331
{
332
struct epair_softc *sc;
333
struct ifnet *oifp;
334
#ifdef ALTQ
335
int len;
336
bool mcast;
337
#endif
338
339
if (m == NULL)
340
return (0);
341
M_ASSERTPKTHDR(m);
342
343
/*
344
* We could just transmit this, but it makes testing easier if we're a
345
* little bit more like real hardware.
346
* Allow just that little bit extra for ethernet (and vlan) headers.
347
*/
348
if (m->m_pkthdr.len > (ifp->if_mtu + sizeof(struct ether_vlan_header))) {
349
m_freem(m);
350
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
351
return (E2BIG);
352
}
353
354
/*
355
* We are not going to use the interface en/dequeue mechanism
356
* on the TX side. We are called from ether_output_frame()
357
* and will put the packet into the receive-queue (rxq) of the
358
* other interface (oifp) of our pair.
359
*/
360
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
361
m_freem(m);
362
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
363
return (ENXIO);
364
}
365
if ((ifp->if_flags & IFF_UP) == 0) {
366
m_freem(m);
367
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
368
return (ENETDOWN);
369
}
370
371
BPF_MTAP(ifp, m);
372
373
/*
374
* In case the outgoing interface is not usable,
375
* drop the packet.
376
*/
377
sc = ifp->if_softc;
378
oifp = sc->oifp;
379
if ((oifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
380
(oifp->if_flags & IFF_UP) == 0) {
381
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
382
m_freem(m);
383
return (0);
384
}
385
386
#ifdef ALTQ
387
len = m->m_pkthdr.len;
388
mcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
389
int error = 0;
390
391
/* Support ALTQ via the classic if_start() path. */
392
IF_LOCK(&ifp->if_snd);
393
if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
394
ALTQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
395
if (error)
396
if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
397
IF_UNLOCK(&ifp->if_snd);
398
if (!error) {
399
if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
400
if (mcast)
401
if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
402
epair_start(ifp);
403
}
404
return (error);
405
}
406
IF_UNLOCK(&ifp->if_snd);
407
#endif
408
409
epair_menq(m, oifp->if_softc);
410
return (0);
411
}
412
413
static void
414
epair_qflush(struct ifnet *ifp __unused)
415
{
416
}
417
418
static int
419
epair_media_change(struct ifnet *ifp __unused)
420
{
421
422
/* Do nothing. */
423
return (0);
424
}
425
426
static void
427
epair_media_status(struct ifnet *ifp __unused, struct ifmediareq *imr)
428
{
429
430
imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
431
imr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
432
}
433
434
/*
435
* Update ifp->if_hwassist according to the current value of ifp->if_capenable.
436
*/
437
static void
438
epair_caps_changed(struct ifnet *ifp)
439
{
440
uint64_t hwassist = 0;
441
442
if (ifp->if_capenable & IFCAP_TXCSUM)
443
hwassist |= CSUM_IP_TCP | CSUM_IP_UDP;
444
if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
445
hwassist |= CSUM_IP6_TCP | CSUM_IP6_UDP;
446
ifp->if_hwassist = hwassist;
447
}
448
449
static int
450
epair_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
451
{
452
struct epair_softc *sc;
453
struct ifreq *ifr;
454
int error;
455
456
ifr = (struct ifreq *)data;
457
switch (cmd) {
458
case SIOCSIFFLAGS:
459
case SIOCADDMULTI:
460
case SIOCDELMULTI:
461
error = 0;
462
break;
463
464
case SIOCSIFMEDIA:
465
case SIOCGIFMEDIA:
466
sc = ifp->if_softc;
467
error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
468
break;
469
470
case SIOCSIFMTU:
471
/* We basically allow all kinds of MTUs. */
472
ifp->if_mtu = ifr->ifr_mtu;
473
error = 0;
474
break;
475
476
case SIOCGIFCAP:
477
ifr->ifr_reqcap = ifp->if_capabilities;
478
ifr->ifr_curcap = ifp->if_capenable;
479
error = 0;
480
break;
481
case SIOCSIFCAP:
482
/*
483
* Enable/disable capabilities as requested, besides
484
* IFCAP_RXCSUM(_IPV6), which always remain enabled.
485
* Incoming packets may have the mbuf flag CSUM_DATA_VALID set.
486
* Without IFCAP_RXCSUM(_IPV6), this flag would have to be
487
* removed, which does not seem helpful.
488
*/
489
ifp->if_capenable = ifr->ifr_reqcap | IFCAP_RXCSUM |
490
IFCAP_RXCSUM_IPV6;
491
epair_caps_changed(ifp);
492
VLAN_CAPABILITIES(ifp);
493
/*
494
* If IFCAP_TXCSUM(_IPV6) has been changed, change it on the
495
* other epair interface as well.
496
* A bridge disables IFCAP_TXCSUM(_IPV6) when adding one epair
497
* interface if another interface in the bridge has it disabled.
498
* In that case this capability needs to be disabled on the
499
* other epair interface to avoid sending packets in the bridge
500
* that rely on this capability.
501
* Do the same for IFCAP_VLAN_HWTAGGING. If the sending epair
502
* end has this capability enabled, the other end has to have
503
* it enabled too. Otherwise, epair would have to add the VLAN
504
* tag in the Ethernet header.
505
*/
506
sc = ifp->if_softc;
507
if ((ifp->if_capenable ^ sc->oifp->if_capenable) &
508
(IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | IFCAP_VLAN_HWTAGGING)) {
509
sc->oifp->if_capenable &=
510
~(IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 |
511
IFCAP_VLAN_HWTAGGING);
512
sc->oifp->if_capenable |= ifp->if_capenable &
513
(IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 |
514
IFCAP_VLAN_HWTAGGING);
515
epair_caps_changed(sc->oifp);
516
VLAN_CAPABILITIES(sc->oifp);
517
}
518
error = 0;
519
break;
520
521
default:
522
/* Let the common ethernet handler process this. */
523
error = ether_ioctl(ifp, cmd, data);
524
break;
525
}
526
527
return (error);
528
}
529
530
static void
531
epair_init(void *dummy __unused)
532
{
533
}
534
535
/*
536
* Interface cloning functions.
537
* We use our private ones so that we can create/destroy our secondary
538
* device along with the primary one.
539
*/
540
static int
541
epair_clone_match(struct if_clone *ifc, const char *name)
542
{
543
const char *cp;
544
545
/*
546
* Our base name is epair.
547
* Our interfaces will be named epair<n>[ab].
548
* So accept anything of the following list:
549
* - epair
550
* - epair<n>
551
* but not the epair<n>[ab] versions.
552
*/
553
if (strncmp(epairname, name, sizeof(epairname)-1) != 0)
554
return (0);
555
556
for (cp = name + sizeof(epairname) - 1; *cp != '\0'; cp++) {
557
if (*cp < '0' || *cp > '9')
558
return (0);
559
}
560
561
return (1);
562
}
563
564
static void
565
epair_generate_mac_byname(struct epair_softc *sc, uint8_t eaddr[])
566
{
567
struct ether_addr gen_eaddr;
568
int i;
569
570
ether_gen_addr_byname(if_name(sc->ifp), &gen_eaddr);
571
for (i = 0; i < ETHER_ADDR_LEN; i++)
572
eaddr[i] = gen_eaddr.octet[i];
573
}
574
575
static void
576
epair_clone_add(struct if_clone *ifc, struct epair_softc *scb)
577
{
578
struct ifnet *ifp;
579
uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
580
581
ifp = scb->ifp;
582
if (!use_ether_gen_addr) {
583
/* Copy epairNa etheraddr and change the last byte. */
584
memcpy(eaddr, scb->oifp->if_hw_addr, ETHER_ADDR_LEN);
585
eaddr[5] = 0x0b;
586
} else
587
epair_generate_mac_byname(scb, eaddr);
588
ether_ifattach(ifp, eaddr);
589
590
if_clone_addif(ifc, ifp);
591
}
592
593
static struct epair_softc *
594
epair_alloc_sc(struct if_clone *ifc)
595
{
596
struct epair_softc *sc;
597
598
struct ifnet *ifp = if_alloc(IFT_ETHER);
599
sc = malloc(sizeof(struct epair_softc), M_EPAIR, M_WAITOK | M_ZERO);
600
sc->ifp = ifp;
601
sc->num_queues = epair_tasks.tasks;
602
sc->queues = mallocarray(sc->num_queues, sizeof(struct epair_queue),
603
M_EPAIR, M_WAITOK);
604
for (int i = 0; i < sc->num_queues; i++) {
605
struct epair_queue *q = &sc->queues[i];
606
q->id = i;
607
q->state = EPAIR_QUEUE_IDLE;
608
mtx_init(&q->mtx, "epairq", NULL, MTX_DEF | MTX_NEW);
609
mbufq_init(&q->q, RXRSIZE);
610
q->sc = sc;
611
NET_TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q);
612
}
613
614
/* Initialise pseudo media types. */
615
ifmedia_init(&sc->media, 0, epair_media_change, epair_media_status);
616
ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
617
ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T);
618
619
return (sc);
620
}
621
622
static void
623
epair_setup_ifp(struct epair_softc *sc, char *name, int unit)
624
{
625
struct ifnet *ifp = sc->ifp;
626
627
ifp->if_softc = sc;
628
strlcpy(ifp->if_xname, name, IFNAMSIZ);
629
ifp->if_dname = epairname;
630
ifp->if_dunit = unit;
631
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
632
ifp->if_capabilities =
633
IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
634
IFCAP_TXCSUM | IFCAP_RXCSUM |
635
IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6;
636
ifp->if_capenable = ifp->if_capabilities;
637
epair_caps_changed(ifp);
638
ifp->if_transmit = epair_transmit;
639
ifp->if_qflush = epair_qflush;
640
ifp->if_start = epair_start;
641
ifp->if_ioctl = epair_ioctl;
642
ifp->if_init = epair_init;
643
if_setsendqlen(ifp, ifqmaxlen);
644
if_setsendqready(ifp);
645
646
ifp->if_baudrate = IF_Gbps(10); /* arbitrary maximum */
647
}
648
649
static void
650
epair_generate_mac(struct epair_softc *sc, uint8_t *eaddr)
651
{
652
uint32_t key[3];
653
uint32_t hash;
654
uint64_t hostid;
655
656
EPAIR_LOCK();
657
#ifdef SMP
658
/* Get an approximate distribution. */
659
hash = next_index % mp_ncpus;
660
#else
661
hash = 0;
662
#endif
663
EPAIR_UNLOCK();
664
665
/*
666
* Calculate the etheraddr hashing the hostid and the
667
* interface index. The result would be hopefully unique.
668
* Note that the "a" component of an epair instance may get moved
669
* to a different VNET after creation. In that case its index
670
* will be freed and the index can get reused by new epair instance.
671
* Make sure we do not create same etheraddr again.
672
*/
673
getcredhostid(curthread->td_ucred, (unsigned long *)&hostid);
674
if (hostid == 0)
675
arc4rand(&hostid, sizeof(hostid), 0);
676
677
struct ifnet *ifp = sc->ifp;
678
EPAIR_LOCK();
679
if (ifp->if_index > next_index)
680
next_index = ifp->if_index;
681
else
682
next_index++;
683
684
key[0] = (uint32_t)next_index;
685
EPAIR_UNLOCK();
686
key[1] = (uint32_t)(hostid & 0xffffffff);
687
key[2] = (uint32_t)((hostid >> 32) & 0xfffffffff);
688
hash = jenkins_hash32(key, 3, 0);
689
690
eaddr[0] = 0x02;
691
memcpy(&eaddr[1], &hash, 4);
692
eaddr[5] = 0x0a;
693
}
694
695
static void
696
epair_free_sc(struct epair_softc *sc)
697
{
698
699
if_free(sc->ifp);
700
ifmedia_removeall(&sc->media);
701
for (int i = 0; i < sc->num_queues; i++) {
702
struct epair_queue *q = &sc->queues[i];
703
mtx_destroy(&q->mtx);
704
}
705
free(sc->queues, M_EPAIR);
706
free(sc, M_EPAIR);
707
}
708
709
static void
710
epair_set_state(struct ifnet *ifp, bool running)
711
{
712
if (running) {
713
ifp->if_drv_flags |= IFF_DRV_RUNNING;
714
if_link_state_change(ifp, LINK_STATE_UP);
715
} else {
716
if_link_state_change(ifp, LINK_STATE_DOWN);
717
ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
718
}
719
}
720
721
static int
722
epair_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
723
{
724
int error = 0, unit, wildcard;
725
char *dp;
726
727
/* Try to see if a special unit was requested. */
728
error = ifc_name2unit(name, &unit);
729
if (error != 0)
730
return (error);
731
wildcard = (unit < 0);
732
733
error = ifc_alloc_unit(ifc, &unit);
734
if (error != 0)
735
return (error);
736
737
/*
738
* If no unit had been given, we need to adjust the ifName.
739
* Also make sure there is space for our extra [ab] suffix.
740
*/
741
for (dp = name; *dp != '\0'; dp++);
742
if (wildcard) {
743
int slen = snprintf(dp, len - (dp - name), "%d", unit);
744
if (slen > len - (dp - name) - 1) {
745
/* ifName too long. */
746
error = ENOSPC;
747
goto done;
748
}
749
dp += slen;
750
}
751
if (len - (dp - name) - 1 < 1) {
752
/* No space left for our [ab] suffix. */
753
error = ENOSPC;
754
goto done;
755
}
756
*dp = 'b';
757
/* Must not change dp so we can replace 'a' by 'b' later. */
758
*(dp+1) = '\0';
759
760
/* Check if 'a' and 'b' interfaces already exist. */
761
if (ifunit(name) != NULL) {
762
error = EEXIST;
763
goto done;
764
}
765
766
*dp = 'a';
767
if (ifunit(name) != NULL) {
768
error = EEXIST;
769
goto done;
770
}
771
*punit = unit;
772
done:
773
if (error != 0)
774
ifc_free_unit(ifc, unit);
775
776
return (error);
777
}
778
779
static int
780
epair_clone_create(struct if_clone *ifc, char *name, size_t len,
781
struct ifc_data *ifd, struct ifnet **ifpp)
782
{
783
struct epair_softc *sca, *scb;
784
struct ifnet *ifp;
785
char *dp;
786
int error, unit;
787
uint8_t eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
788
789
error = epair_handle_unit(ifc, name, len, &unit);
790
if (error != 0)
791
return (error);
792
793
/* Allocate memory for both [ab] interfaces */
794
sca = epair_alloc_sc(ifc);
795
scb = epair_alloc_sc(ifc);
796
797
/*
798
* Cross-reference the interfaces so we will be able to free both.
799
*/
800
sca->oifp = scb->ifp;
801
scb->oifp = sca->ifp;
802
803
/* Finish initialization of interface <n>a. */
804
ifp = sca->ifp;
805
epair_setup_ifp(sca, name, unit);
806
if (!use_ether_gen_addr)
807
epair_generate_mac(sca, eaddr);
808
else
809
epair_generate_mac_byname(sca, eaddr);
810
811
ether_ifattach(ifp, eaddr);
812
813
/* Swap the name and finish initialization of interface <n>b. */
814
dp = name + strlen(name) - 1;
815
*dp = 'b';
816
817
epair_setup_ifp(scb, name, unit);
818
819
ifp = scb->ifp;
820
/* We need to play some tricks here for the second interface. */
821
strlcpy(name, epairname, len);
822
/* Correctly set the name for the cloner list. */
823
strlcpy(name, scb->ifp->if_xname, len);
824
825
epair_clone_add(ifc, scb);
826
827
/*
828
* Restore name to <n>a as the ifp for this will go into the
829
* cloner list for the initial call.
830
*/
831
strlcpy(name, sca->ifp->if_xname, len);
832
833
/* Tell the world, that we are ready to rock. */
834
epair_set_state(sca->ifp, true);
835
epair_set_state(scb->ifp, true);
836
837
*ifpp = sca->ifp;
838
839
return (0);
840
}
841
842
static void
843
epair_drain_rings(struct epair_softc *sc)
844
{
845
for (int i = 0; i < sc->num_queues; i++) {
846
struct epair_queue *q;
847
struct mbuf *m, *n;
848
849
q = &sc->queues[i];
850
mtx_lock(&q->mtx);
851
m = mbufq_flush(&q->q);
852
mtx_unlock(&q->mtx);
853
854
for (; m != NULL; m = n) {
855
n = m->m_nextpkt;
856
m_freem(m);
857
}
858
}
859
}
860
861
static int
862
epair_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
863
{
864
struct ifnet *oifp;
865
struct epair_softc *sca, *scb;
866
int unit, error;
867
868
/*
869
* In case we called into if_clone_destroyif() ourselves
870
* again to remove the second interface, the softc will be
871
* NULL. In that case so not do anything but return success.
872
*/
873
if (ifp->if_softc == NULL)
874
return (0);
875
876
unit = ifp->if_dunit;
877
sca = ifp->if_softc;
878
oifp = sca->oifp;
879
scb = oifp->if_softc;
880
881
/* Frist get the interfaces down and detached. */
882
epair_set_state(ifp, false);
883
epair_set_state(oifp, false);
884
885
ether_ifdetach(ifp);
886
ether_ifdetach(oifp);
887
888
/* Third free any queued packets and all the resources. */
889
CURVNET_SET_QUIET(oifp->if_vnet);
890
epair_drain_rings(scb);
891
oifp->if_softc = NULL;
892
error = if_clone_destroyif(ifc, oifp);
893
if (error)
894
panic("%s: if_clone_destroyif() for our 2nd iface failed: %d",
895
__func__, error);
896
epair_free_sc(scb);
897
CURVNET_RESTORE();
898
899
epair_drain_rings(sca);
900
epair_free_sc(sca);
901
902
/* Last free the cloner unit. */
903
ifc_free_unit(ifc, unit);
904
905
return (0);
906
}
907
908
static void
909
vnet_epair_init(const void *unused __unused)
910
{
911
struct if_clone_addreq req = {
912
.match_f = epair_clone_match,
913
.create_f = epair_clone_create,
914
.destroy_f = epair_clone_destroy,
915
};
916
V_epair_cloner = ifc_attach_cloner(epairname, &req);
917
}
918
VNET_SYSINIT(vnet_epair_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
919
vnet_epair_init, NULL);
920
921
static void
922
vnet_epair_uninit(const void *unused __unused)
923
{
924
925
ifc_detach_cloner(V_epair_cloner);
926
}
927
VNET_SYSUNINIT(vnet_epair_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
928
vnet_epair_uninit, NULL);
929
930
static int
931
epair_mod_init(void)
932
{
933
char name[32];
934
epair_tasks.tasks = 0;
935
936
#ifdef RSS
937
int cpu;
938
939
CPU_FOREACH(cpu) {
940
cpuset_t cpu_mask;
941
942
/* Pin to this CPU so we get appropriate NUMA allocations. */
943
thread_lock(curthread);
944
sched_bind(curthread, cpu);
945
thread_unlock(curthread);
946
947
snprintf(name, sizeof(name), "epair_task_%d", cpu);
948
949
epair_tasks.tq[cpu] = taskqueue_create(name, M_WAITOK,
950
taskqueue_thread_enqueue,
951
&epair_tasks.tq[cpu]);
952
CPU_SETOF(cpu, &cpu_mask);
953
taskqueue_start_threads_cpuset(&epair_tasks.tq[cpu], 1, PI_NET,
954
&cpu_mask, "%s", name);
955
956
epair_tasks.tasks++;
957
}
958
thread_lock(curthread);
959
sched_unbind(curthread);
960
thread_unlock(curthread);
961
#else
962
snprintf(name, sizeof(name), "epair_task");
963
964
epair_tasks.tq[0] = taskqueue_create(name, M_WAITOK,
965
taskqueue_thread_enqueue,
966
&epair_tasks.tq[0]);
967
taskqueue_start_threads(&epair_tasks.tq[0], 1, PI_NET, "%s", name);
968
969
epair_tasks.tasks = 1;
970
#endif
971
972
return (0);
973
}
974
975
static void
976
epair_mod_cleanup(void)
977
{
978
979
for (int i = 0; i < epair_tasks.tasks; i++) {
980
taskqueue_drain_all(epair_tasks.tq[i]);
981
taskqueue_free(epair_tasks.tq[i]);
982
}
983
}
984
985
static int
986
epair_modevent(module_t mod, int type, void *data)
987
{
988
int ret;
989
990
switch (type) {
991
case MOD_LOAD:
992
EPAIR_LOCK_INIT();
993
ret = epair_mod_init();
994
if (ret != 0)
995
return (ret);
996
if (bootverbose)
997
printf("%s: %s initialized.\n", __func__, epairname);
998
break;
999
case MOD_UNLOAD:
1000
epair_mod_cleanup();
1001
EPAIR_LOCK_DESTROY();
1002
if (bootverbose)
1003
printf("%s: %s unloaded.\n", __func__, epairname);
1004
break;
1005
default:
1006
return (EOPNOTSUPP);
1007
}
1008
return (0);
1009
}
1010
1011
static moduledata_t epair_mod = {
1012
"if_epair",
1013
epair_modevent,
1014
0
1015
};
1016
1017
DECLARE_MODULE(if_epair, epair_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
1018
MODULE_VERSION(if_epair, 3);
1019
1020