1/*-
2 * Copyright (c) 2016 Alexander Motin <mav@FreeBSD.org>
3 * Copyright (C) 2013 Intel Corporation
4 * Copyright (C) 2015 EMC Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * The Non-Transparent Bridge (NTB) is a device that allows you to connect
31 * two or more systems using a PCI-e links, providing remote memory access.
32 *
33 * This module contains a driver for simulated Ethernet device, using
34 * underlying NTB Transport device.
35 *
36 * NOTE: Much of the code in this module is shared with Linux. Any patches may
37 * be picked up and redistributed in Linux with a dual GPL/BSD license.
38 */
39
40#include <sys/param.h>
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/buf_ring.h>
44#include <sys/bus.h>
45#include <sys/ktr.h>
46#include <sys/limits.h>
47#include <sys/module.h>
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <sys/sysctl.h>
51#include <sys/taskqueue.h>
52
53#include <net/if.h>
54#include <net/if_media.h>
55#include <net/if_types.h>
56#include <net/if_media.h>
57#include <net/if_var.h>
58#include <net/bpf.h>
59#include <net/ethernet.h>
60
61#include <machine/bus.h>
62
63#include "../ntb_transport.h"
64
65#define KTR_NTB KTR_SPARE3
66#define NTB_MEDIATYPE		 (IFM_ETHER | IFM_AUTO | IFM_FDX)
67
68#define	NTB_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
69#define	NTB_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
70#define	NTB_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
71				    CSUM_PSEUDO_HDR | \
72				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
73				    CSUM_SCTP_VALID)
74
75static SYSCTL_NODE(_hw, OID_AUTO, if_ntb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76    "if_ntb");
77
78static unsigned g_if_ntb_num_queues = UINT_MAX;
79SYSCTL_UINT(_hw_if_ntb, OID_AUTO, num_queues, CTLFLAG_RWTUN,
80    &g_if_ntb_num_queues, 0, "Number of queues per interface");
81
82struct ntb_net_queue {
83	struct ntb_net_ctx	*sc;
84	if_t			 ifp;
85	struct ntb_transport_qp *qp;
86	struct buf_ring		*br;
87	struct task		 tx_task;
88	struct taskqueue	*tx_tq;
89	struct mtx		 tx_lock;
90	struct callout		 queue_full;
91};
92
93struct ntb_net_ctx {
94	if_t			 ifp;
95	struct ifmedia		 media;
96	u_char			 eaddr[ETHER_ADDR_LEN];
97	int			 num_queues;
98	struct ntb_net_queue	*queues;
99	int			 mtu;
100};
101
102static int ntb_net_probe(device_t dev);
103static int ntb_net_attach(device_t dev);
104static int ntb_net_detach(device_t dev);
105static void ntb_net_init(void *arg);
106static int ntb_ifmedia_upd(struct ifnet *);
107static void ntb_ifmedia_sts(struct ifnet *, struct ifmediareq *);
108static int ntb_ioctl(if_t ifp, u_long command, caddr_t data);
109static int ntb_transmit(if_t ifp, struct mbuf *m);
110static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
111    void *data, int len);
112static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
113    void *data, int len);
114static void ntb_net_event_handler(void *data, enum ntb_link_event status);
115static void ntb_handle_tx(void *arg, int pending);
116static void ntb_qp_full(void *arg);
117static void ntb_qflush(if_t ifp);
118static void create_random_local_eui48(u_char *eaddr);
119
120static int
121ntb_net_probe(device_t dev)
122{
123
124	device_set_desc(dev, "NTB Network Interface");
125	return (0);
126}
127
128static int
129ntb_net_attach(device_t dev)
130{
131	struct ntb_net_ctx *sc = device_get_softc(dev);
132	struct ntb_net_queue *q;
133	if_t ifp;
134	struct ntb_queue_handlers handlers = { ntb_net_rx_handler,
135	    ntb_net_tx_handler, ntb_net_event_handler };
136	int i;
137
138	ifp = sc->ifp = if_gethandle(IFT_ETHER);
139	if (ifp == NULL) {
140		printf("ntb: Cannot allocate ifnet structure\n");
141		return (ENOMEM);
142	}
143	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
144	if_setdev(ifp, dev);
145
146	sc->num_queues = min(g_if_ntb_num_queues,
147	    ntb_transport_queue_count(dev));
148	sc->queues = malloc(sc->num_queues * sizeof(struct ntb_net_queue),
149	    M_DEVBUF, M_WAITOK | M_ZERO);
150	sc->mtu = INT_MAX;
151	for (i = 0; i < sc->num_queues; i++) {
152		q = &sc->queues[i];
153		q->sc = sc;
154		q->ifp = ifp;
155		q->qp = ntb_transport_create_queue(dev, i, &handlers, q);
156		if (q->qp == NULL)
157			break;
158		sc->mtu = imin(sc->mtu, ntb_transport_max_size(q->qp));
159		mtx_init(&q->tx_lock, "ntb tx", NULL, MTX_DEF);
160		q->br = buf_ring_alloc(4096, M_DEVBUF, M_WAITOK, &q->tx_lock);
161		TASK_INIT(&q->tx_task, 0, ntb_handle_tx, q);
162		q->tx_tq = taskqueue_create_fast("ntb_txq", M_NOWAIT,
163		    taskqueue_thread_enqueue, &q->tx_tq);
164		taskqueue_start_threads(&q->tx_tq, 1, PI_NET, "%s txq%d",
165		    device_get_nameunit(dev), i);
166		callout_init(&q->queue_full, 1);
167	}
168	sc->num_queues = i;
169	device_printf(dev, "%d queue(s)\n", sc->num_queues);
170
171	if_setinitfn(ifp, ntb_net_init);
172	if_setsoftc(ifp, sc);
173	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
174	if_setioctlfn(ifp, ntb_ioctl);
175	if_settransmitfn(ifp, ntb_transmit);
176	if_setqflushfn(ifp, ntb_qflush);
177	create_random_local_eui48(sc->eaddr);
178	ether_ifattach(ifp, sc->eaddr);
179	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 |
180	    IFCAP_JUMBO_MTU | IFCAP_LINKSTATE);
181	if_setcapenable(ifp, IFCAP_JUMBO_MTU | IFCAP_LINKSTATE);
182	if_setmtu(ifp, sc->mtu - ETHER_HDR_LEN);
183
184	ifmedia_init(&sc->media, IFM_IMASK, ntb_ifmedia_upd,
185	    ntb_ifmedia_sts);
186	ifmedia_add(&sc->media, NTB_MEDIATYPE, 0, NULL);
187	ifmedia_set(&sc->media, NTB_MEDIATYPE);
188
189	for (i = 0; i < sc->num_queues; i++)
190		ntb_transport_link_up(sc->queues[i].qp);
191	return (0);
192}
193
194static int
195ntb_net_detach(device_t dev)
196{
197	struct ntb_net_ctx *sc = device_get_softc(dev);
198	struct ntb_net_queue *q;
199	int i;
200
201	for (i = 0; i < sc->num_queues; i++)
202		ntb_transport_link_down(sc->queues[i].qp);
203	ether_ifdetach(sc->ifp);
204	if_free(sc->ifp);
205	ifmedia_removeall(&sc->media);
206	for (i = 0; i < sc->num_queues; i++) {
207		q = &sc->queues[i];
208		ntb_transport_free_queue(q->qp);
209		buf_ring_free(q->br, M_DEVBUF);
210		callout_drain(&q->queue_full);
211		taskqueue_drain_all(q->tx_tq);
212		mtx_destroy(&q->tx_lock);
213	}
214	free(sc->queues, M_DEVBUF);
215	return (0);
216}
217
218/* Network device interface */
219
220static void
221ntb_net_init(void *arg)
222{
223	struct ntb_net_ctx *sc = arg;
224	if_t ifp = sc->ifp;
225
226	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
227	if_setbaudrate(ifp, ntb_transport_link_speed(sc->queues[0].qp));
228	if_link_state_change(ifp, ntb_transport_link_query(sc->queues[0].qp) ?
229	    LINK_STATE_UP : LINK_STATE_DOWN);
230}
231
232static int
233ntb_ioctl(if_t ifp, u_long command, caddr_t data)
234{
235	struct ntb_net_ctx *sc = if_getsoftc(ifp);
236	struct ifreq *ifr = (struct ifreq *)data;
237	int error = 0;
238
239	switch (command) {
240	case SIOCSIFFLAGS:
241	case SIOCADDMULTI:
242	case SIOCDELMULTI:
243		break;
244
245	case SIOCSIFMTU:
246	    {
247		if (ifr->ifr_mtu > sc->mtu - ETHER_HDR_LEN) {
248			error = EINVAL;
249			break;
250		}
251
252		if_setmtu(ifp, ifr->ifr_mtu);
253		break;
254	    }
255
256	case SIOCSIFMEDIA:
257	case SIOCGIFMEDIA:
258		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
259		break;
260
261	case SIOCSIFCAP:
262		if (ifr->ifr_reqcap & IFCAP_RXCSUM)
263			if_setcapenablebit(ifp, IFCAP_RXCSUM, 0);
264		else
265			if_setcapenablebit(ifp, 0, IFCAP_RXCSUM);
266		if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
267			if_setcapenablebit(ifp, IFCAP_TXCSUM, 0);
268			if_sethwassistbits(ifp, NTB_CSUM_FEATURES, 0);
269		} else {
270			if_setcapenablebit(ifp, 0, IFCAP_TXCSUM);
271			if_sethwassistbits(ifp, 0, NTB_CSUM_FEATURES);
272		}
273		if (ifr->ifr_reqcap & IFCAP_RXCSUM_IPV6)
274			if_setcapenablebit(ifp, IFCAP_RXCSUM_IPV6, 0);
275		else
276			if_setcapenablebit(ifp, 0, IFCAP_RXCSUM_IPV6);
277		if (ifr->ifr_reqcap & IFCAP_TXCSUM_IPV6) {
278			if_setcapenablebit(ifp, IFCAP_TXCSUM_IPV6, 0);
279			if_sethwassistbits(ifp, NTB_CSUM_FEATURES6, 0);
280		} else {
281			if_setcapenablebit(ifp, 0, IFCAP_TXCSUM_IPV6);
282			if_sethwassistbits(ifp, 0, NTB_CSUM_FEATURES6);
283		}
284		break;
285
286	default:
287		error = ether_ioctl(ifp, command, data);
288		break;
289	}
290
291	return (error);
292}
293
294static int
295ntb_ifmedia_upd(struct ifnet *ifp)
296{
297	struct ntb_net_ctx *sc = if_getsoftc(ifp);
298	struct ifmedia *ifm = &sc->media;
299
300	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
301		return (EINVAL);
302
303	return (0);
304}
305
306static void
307ntb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
308{
309	struct ntb_net_ctx *sc = if_getsoftc(ifp);
310
311	ifmr->ifm_status = IFM_AVALID;
312	ifmr->ifm_active = NTB_MEDIATYPE;
313	if (ntb_transport_link_query(sc->queues[0].qp))
314		ifmr->ifm_status |= IFM_ACTIVE;
315}
316
317static void
318ntb_transmit_locked(struct ntb_net_queue *q)
319{
320	if_t ifp = q->ifp;
321	struct mbuf *m;
322	int rc, len;
323	short mflags;
324
325	CTR0(KTR_NTB, "TX: ntb_transmit_locked");
326	while ((m = drbr_peek(ifp, q->br)) != NULL) {
327		CTR1(KTR_NTB, "TX: start mbuf %p", m);
328		ether_bpf_mtap_if(ifp, m);
329		len = m->m_pkthdr.len;
330		mflags = m->m_flags;
331		rc = ntb_transport_tx_enqueue(q->qp, m, m, len);
332		if (rc != 0) {
333			CTR2(KTR_NTB, "TX: could not tx mbuf %p: %d", m, rc);
334			if (rc == EAGAIN) {
335				drbr_putback(ifp, q->br, m);
336				callout_reset_sbt(&q->queue_full,
337				    SBT_1MS / 4, SBT_1MS / 4,
338				    ntb_qp_full, q, 0);
339			} else {
340				m_freem(m);
341				drbr_advance(ifp, q->br);
342				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
343			}
344			break;
345		}
346		drbr_advance(ifp, q->br);
347		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
348		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
349		if (mflags & M_MCAST)
350			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
351	}
352}
353
354static int
355ntb_transmit(if_t ifp, struct mbuf *m)
356{
357	struct ntb_net_ctx *sc = if_getsoftc(ifp);
358	struct ntb_net_queue *q;
359	int error, i;
360
361	CTR0(KTR_NTB, "TX: ntb_transmit");
362	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
363		i = m->m_pkthdr.flowid % sc->num_queues;
364	else
365		i = curcpu % sc->num_queues;
366	q = &sc->queues[i];
367
368	error = drbr_enqueue(ifp, q->br, m);
369	if (error)
370		return (error);
371
372	if (mtx_trylock(&q->tx_lock)) {
373		ntb_transmit_locked(q);
374		mtx_unlock(&q->tx_lock);
375	} else
376		taskqueue_enqueue(q->tx_tq, &q->tx_task);
377	return (0);
378}
379
380static void
381ntb_handle_tx(void *arg, int pending)
382{
383	struct ntb_net_queue *q = arg;
384
385	mtx_lock(&q->tx_lock);
386	ntb_transmit_locked(q);
387	mtx_unlock(&q->tx_lock);
388}
389
390static void
391ntb_qp_full(void *arg)
392{
393	struct ntb_net_queue *q = arg;
394
395	CTR0(KTR_NTB, "TX: qp_full callout");
396	if (ntb_transport_tx_free_entry(q->qp) > 0)
397		taskqueue_enqueue(q->tx_tq, &q->tx_task);
398	else
399		callout_schedule_sbt(&q->queue_full,
400		    SBT_1MS / 4, SBT_1MS / 4, 0);
401}
402
403static void
404ntb_qflush(if_t ifp)
405{
406	struct ntb_net_ctx *sc = if_getsoftc(ifp);
407	struct ntb_net_queue *q;
408	struct mbuf *m;
409	int i;
410
411	for (i = 0; i < sc->num_queues; i++) {
412		q = &sc->queues[i];
413		mtx_lock(&q->tx_lock);
414		while ((m = buf_ring_dequeue_sc(q->br)) != NULL)
415			m_freem(m);
416		mtx_unlock(&q->tx_lock);
417	}
418	if_qflush(ifp);
419}
420
421/* Network Device Callbacks */
422static void
423ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
424    int len)
425{
426
427	m_freem(data);
428	CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data);
429}
430
431static void
432ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
433    int len)
434{
435	struct ntb_net_queue *q = qp_data;
436	struct ntb_net_ctx *sc = q->sc;
437	struct mbuf *m = data;
438	if_t ifp = q->ifp;
439	uint16_t proto;
440
441	CTR1(KTR_NTB, "RX: rx handler (%d)", len);
442	if (len < 0) {
443		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
444		return;
445	}
446
447	m->m_pkthdr.rcvif = ifp;
448	if (sc->num_queues > 1) {
449		m->m_pkthdr.flowid = q - sc->queues;
450		M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
451	}
452	if (if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
453		m_copydata(m, 12, 2, (void *)&proto);
454		switch (ntohs(proto)) {
455		case ETHERTYPE_IP:
456			if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
457				m->m_pkthdr.csum_data = 0xffff;
458				m->m_pkthdr.csum_flags = NTB_CSUM_SET;
459			}
460			break;
461		case ETHERTYPE_IPV6:
462			if (if_getcapenable(ifp) & IFCAP_RXCSUM_IPV6) {
463				m->m_pkthdr.csum_data = 0xffff;
464				m->m_pkthdr.csum_flags = NTB_CSUM_SET;
465			}
466			break;
467		}
468	}
469	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
470	if_input(ifp, m);
471}
472
473static void
474ntb_net_event_handler(void *data, enum ntb_link_event status)
475{
476	struct ntb_net_queue *q = data;
477
478	if_setbaudrate(q->ifp, ntb_transport_link_speed(q->qp));
479	if_link_state_change(q->ifp, (status == NTB_LINK_UP) ? LINK_STATE_UP :
480	    LINK_STATE_DOWN);
481}
482
483/* Helper functions */
484/* TODO: This too should really be part of the kernel */
485#define EUI48_MULTICAST			1 << 0
486#define EUI48_LOCALLY_ADMINISTERED	1 << 1
487static void
488create_random_local_eui48(u_char *eaddr)
489{
490	static uint8_t counter = 0;
491
492	eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
493	arc4rand(&eaddr[1], 4, 0);
494	eaddr[5] = counter++;
495}
496
497static device_method_t ntb_net_methods[] = {
498	/* Device interface */
499	DEVMETHOD(device_probe,     ntb_net_probe),
500	DEVMETHOD(device_attach,    ntb_net_attach),
501	DEVMETHOD(device_detach,    ntb_net_detach),
502	DEVMETHOD_END
503};
504
505static DEFINE_CLASS_0(ntb, ntb_net_driver, ntb_net_methods,
506    sizeof(struct ntb_net_ctx));
507DRIVER_MODULE(if_ntb, ntb_transport, ntb_net_driver, NULL, NULL);
508MODULE_DEPEND(if_ntb, ntb_transport, 1, 1, 1);
509MODULE_VERSION(if_ntb, 1);
510