xref: /freebsd/sys/dev/netmap/if_vtnet_netmap.h (revision 4d9488674f3a36a2d12d11cad170cb1bd1c14a6d)
1 /*
2  * Copyright (C) 2014-2018 Vincenzo Maffione, Luigi Rizzo.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * $FreeBSD$
28  */
29 
30 #include <net/netmap.h>
31 #include <sys/selinfo.h>
32 #include <vm/vm.h>
33 #include <vm/pmap.h>    /* vtophys ? */
34 #include <dev/netmap/netmap_kern.h>
35 
36 /* Register and unregister. */
37 static int
38 vtnet_netmap_reg(struct netmap_adapter *na, int state)
39 {
40 	struct ifnet *ifp = na->ifp;
41 	struct vtnet_softc *sc = ifp->if_softc;
42 
43 	/*
44 	 * Trigger a device reinit, asking vtnet_init_locked() to
45 	 * also enter or exit netmap mode.
46 	 */
47 	VTNET_CORE_LOCK(sc);
48 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
49 	vtnet_init_locked(sc, state ? VTNET_INIT_NETMAP_ENTER
50 	    : VTNET_INIT_NETMAP_EXIT);
51 	VTNET_CORE_UNLOCK(sc);
52 
53 	return (0);
54 }
55 
56 
57 /* Reconcile kernel and user view of the transmit ring. */
58 static int
59 vtnet_netmap_txsync(struct netmap_kring *kring, int flags)
60 {
61 	struct netmap_adapter *na = kring->na;
62 	struct ifnet *ifp = na->ifp;
63 	struct netmap_ring *ring = kring->ring;
64 	u_int ring_nr = kring->ring_id;
65 	u_int nm_i;	/* index into the netmap ring */
66 	u_int const lim = kring->nkr_num_slots - 1;
67 	u_int const head = kring->rhead;
68 
69 	/* device-specific */
70 	struct vtnet_softc *sc = ifp->if_softc;
71 	struct vtnet_txq *txq = &sc->vtnet_txqs[ring_nr];
72 	struct virtqueue *vq = txq->vtntx_vq;
73 	int interrupts = !(kring->nr_kflags & NKR_NOINTR);
74 	u_int n;
75 
76 	/*
77 	 * First part: process new packets to send.
78 	 */
79 
80 	nm_i = kring->nr_hwcur;
81 	if (nm_i != head) {	/* we have new packets to send */
82 		struct sglist *sg = txq->vtntx_sg;
83 
84 		for (; nm_i != head; nm_i = nm_next(nm_i, lim)) {
85 			/* we use an empty header here */
86 			struct netmap_slot *slot = &ring->slot[nm_i];
87 			uint64_t offset = nm_get_offset(kring, slot);
88 			u_int len = slot->len;
89 			uint64_t paddr;
90 			void *addr = PNMB(na, slot, &paddr);
91 			int err;
92 
93 			(void)addr;
94 			NM_CHECK_ADDR_LEN_OFF(na, len, offset);
95 
96 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
97 			/* Initialize the scatterlist, expose it to the hypervisor,
98 			 * and kick the hypervisor (if necessary).
99 			 */
100 			sglist_reset(sg); // cheap
101 			err = sglist_append(sg, &txq->vtntx_shrhdr, sc->vtnet_hdr_size);
102 			err |= sglist_append_phys(sg, paddr + offset, len);
103 			KASSERT(err == 0, ("%s: cannot append to sglist %d",
104 						__func__, err));
105 			err = virtqueue_enqueue(vq, /*cookie=*/txq, sg,
106 						/*readable=*/sg->sg_nseg,
107 						/*writeable=*/0);
108 			if (unlikely(err)) {
109 				if (err != ENOSPC)
110 					nm_prerr("virtqueue_enqueue(%s) failed: %d",
111 							kring->name, err);
112 				break;
113 			}
114 		}
115 
116 		virtqueue_notify(vq);
117 
118 		/* Update hwcur depending on where we stopped. */
119 		kring->nr_hwcur = nm_i; /* note we might break early */
120 	}
121 
122 	/* Free used slots. We only consider our own used buffers, recognized
123 	 * by the token we passed to virtqueue_enqueue.
124 	 */
125 	n = 0;
126 	for (;;) {
127 		void *token = virtqueue_dequeue(vq, NULL);
128 		if (token == NULL)
129 			break;
130 		if (unlikely(token != (void *)txq))
131 			nm_prerr("BUG: TX token mismatch");
132 		else
133 			n++;
134 	}
135 	if (n > 0) {
136 		kring->nr_hwtail += n;
137 		if (kring->nr_hwtail > lim)
138 			kring->nr_hwtail -= lim + 1;
139 	}
140 
141 	if (interrupts && virtqueue_nfree(vq) < 32)
142 		virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG);
143 
144 	return 0;
145 }
146 
147 /*
148  * Publish 'num 'netmap receive buffers to the host, starting
149  * from the next available one (rx->vtnrx_nm_refill).
150  * Return a positive error code on error, and 0 on success.
151  * If we could not publish all of the buffers that's an error,
152  * since the netmap ring and the virtqueue would go out of sync.
153  */
154 static int
155 vtnet_netmap_kring_refill(struct netmap_kring *kring, u_int num)
156 {
157 	struct netmap_adapter *na = kring->na;
158 	struct ifnet *ifp = na->ifp;
159 	struct netmap_ring *ring = kring->ring;
160 	u_int ring_nr = kring->ring_id;
161 	u_int const lim = kring->nkr_num_slots - 1;
162 	u_int nm_i;
163 
164 	/* device-specific */
165 	struct vtnet_softc *sc = ifp->if_softc;
166 	struct vtnet_rxq *rxq = &sc->vtnet_rxqs[ring_nr];
167 	struct virtqueue *vq = rxq->vtnrx_vq;
168 
169 	/* use a local sglist, default might be short */
170 	struct sglist_seg ss[2];
171 	struct sglist sg = { ss, 0, 0, 2 };
172 
173 	for (nm_i = rxq->vtnrx_nm_refill; num > 0;
174 	    nm_i = nm_next(nm_i, lim), num--) {
175 		struct netmap_slot *slot = &ring->slot[nm_i];
176 		uint64_t offset = nm_get_offset(kring, slot);
177 		uint64_t paddr;
178 		void *addr = PNMB(na, slot, &paddr);
179 		int err;
180 
181 		if (addr == NETMAP_BUF_BASE(na)) { /* bad buf */
182 			netmap_ring_reinit(kring);
183 			return EFAULT;
184 		}
185 
186 		slot->flags &= ~NS_BUF_CHANGED;
187 		sglist_reset(&sg);
188 		err = sglist_append(&sg, &rxq->vtnrx_shrhdr, sc->vtnet_hdr_size);
189 		err |= sglist_append_phys(&sg, paddr + offset,
190 		    NETMAP_BUF_SIZE(na) - offset);
191 		KASSERT(err == 0, ("%s: cannot append to sglist %d",
192 					__func__, err));
193 		/* writable for the host */
194 		err = virtqueue_enqueue(vq, /*cookie=*/rxq, &sg,
195 				/*readable=*/0, /*writeable=*/sg.sg_nseg);
196 		if (unlikely(err)) {
197 			nm_prerr("virtqueue_enqueue(%s) failed: %d",
198 				kring->name, err);
199 			break;
200 		}
201 	}
202 	rxq->vtnrx_nm_refill = nm_i;
203 
204 	return num == 0 ? 0 : ENOSPC;
205 }
206 
207 /*
208  * Publish netmap buffers on a RX virtqueue.
209  * Returns -1 if this virtqueue is not being opened in netmap mode.
210  * If the virtqueue is being opened in netmap mode, return 0 on success and
211  * a positive error code on failure.
212  */
213 static int
214 vtnet_netmap_rxq_populate(struct vtnet_rxq *rxq)
215 {
216 	struct netmap_adapter *na = NA(rxq->vtnrx_sc->vtnet_ifp);
217 	struct netmap_kring *kring;
218 	struct netmap_slot *slot;
219 	int error;
220 	int num;
221 
222 	slot = netmap_reset(na, NR_RX, rxq->vtnrx_id, 0);
223 	if (slot == NULL)
224 		return -1;
225 	kring = na->rx_rings[rxq->vtnrx_id];
226 
227 	/*
228 	 * Expose all the RX netmap buffers we can. In case of no indirect
229 	 * buffers, the number of netmap slots in the RX ring matches the
230 	 * maximum number of 2-elements sglist that the RX virtqueue can
231 	 * accommodate. We need to start from kring->nr_hwtail, which is 0
232 	 * on the first netmap register and may be different from 0 if a
233 	 * virtio re-init (caused by a netma register or i.e., ifconfig)
234 	 * happens while the device is in use by netmap.
235 	 */
236 	rxq->vtnrx_nm_refill = kring->nr_hwtail;
237 	num = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
238 	error = vtnet_netmap_kring_refill(kring, num);
239 	virtqueue_notify(rxq->vtnrx_vq);
240 
241 	return error;
242 }
243 
244 /* Reconcile kernel and user view of the receive ring. */
245 static int
246 vtnet_netmap_rxsync(struct netmap_kring *kring, int flags)
247 {
248 	struct netmap_adapter *na = kring->na;
249 	struct ifnet *ifp = na->ifp;
250 	struct netmap_ring *ring = kring->ring;
251 	u_int ring_nr = kring->ring_id;
252 	u_int nm_i;	/* index into the netmap ring */
253 	u_int const lim = kring->nkr_num_slots - 1;
254 	u_int const head = kring->rhead;
255 	int force_update = (flags & NAF_FORCE_READ) ||
256 				(kring->nr_kflags & NKR_PENDINTR);
257 	int interrupts = !(kring->nr_kflags & NKR_NOINTR);
258 
259 	/* device-specific */
260 	struct vtnet_softc *sc = ifp->if_softc;
261 	struct vtnet_rxq *rxq = &sc->vtnet_rxqs[ring_nr];
262 	struct virtqueue *vq = rxq->vtnrx_vq;
263 
264 	/*
265 	 * First part: import newly received packets.
266 	 * Only accept our own buffers (matching the token). We should only get
267 	 * matching buffers. The hwtail should never overrun hwcur, because
268 	 * we publish only N-1 receive buffers (and not N).
269 	 * In any case we must not leave this routine with the interrupts
270 	 * disabled, pending packets in the VQ and hwtail == (hwcur - 1),
271 	 * otherwise the pending packets could stall.
272 	 */
273 	if (netmap_no_pendintr || force_update) {
274 		uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim);
275 		void *token;
276 
277 		vtnet_rxq_disable_intr(rxq);
278 
279 		nm_i = kring->nr_hwtail;
280 		for (;;) {
281 			int len;
282 			token = virtqueue_dequeue(vq, &len);
283 			if (token == NULL) {
284 				/*
285 				 * Enable the interrupts again and double-check
286 				 * for more work. We can go on until we win the
287 				 * race condition, since we are not replenishing
288 				 * in the meanwhile, and thus we will process at
289 				 * most N-1 slots.
290 				 */
291 				if (interrupts && vtnet_rxq_enable_intr(rxq)) {
292 					vtnet_rxq_disable_intr(rxq);
293 					continue;
294 				}
295 				break;
296 			}
297 			if (unlikely(token != (void *)rxq)) {
298 				nm_prerr("BUG: RX token mismatch");
299 			} else {
300 				if (nm_i == hwtail_lim) {
301 					KASSERT(false, ("hwtail would "
302 					    "overrun hwcur"));
303 				}
304 
305 				/* Skip the virtio-net header. */
306 				len -= sc->vtnet_hdr_size;
307 				if (unlikely(len < 0)) {
308 					nm_prlim(1, "Truncated virtio-net-header, "
309 						"missing %d bytes", -len);
310 					len = 0;
311 				}
312 				ring->slot[nm_i].len = len;
313 				ring->slot[nm_i].flags = 0;
314 				nm_i = nm_next(nm_i, lim);
315 			}
316 		}
317 		kring->nr_hwtail = nm_i;
318 		kring->nr_kflags &= ~NKR_PENDINTR;
319 	}
320 
321 	/*
322 	 * Second part: skip past packets that userspace has released.
323 	 */
324 	nm_i = kring->nr_hwcur; /* netmap ring index */
325 	if (nm_i != head) {
326 		int released;
327 		int error;
328 
329 		released = head - nm_i;
330 		if (released < 0)
331 			released += kring->nkr_num_slots;
332 		error = vtnet_netmap_kring_refill(kring, released);
333 		if (error) {
334 			nm_prerr("Failed to replenish RX VQ with %u sgs",
335 			    released);
336 			return error;
337 		}
338 		kring->nr_hwcur = head;
339 		virtqueue_notify(vq);
340 	}
341 
342 	nm_prdis("h %d c %d t %d hwcur %d hwtail %d", kring->rhead,
343 	    kring->rcur, kring->rtail, kring->nr_hwcur, kring->nr_hwtail);
344 
345 	return 0;
346 }
347 
348 
349 /* Enable/disable interrupts on all virtqueues. */
350 static void
351 vtnet_netmap_intr(struct netmap_adapter *na, int state)
352 {
353 	struct vtnet_softc *sc = na->ifp->if_softc;
354 	int i;
355 
356 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
357 		struct vtnet_rxq *rxq = &sc->vtnet_rxqs[i];
358 		struct vtnet_txq *txq = &sc->vtnet_txqs[i];
359 		struct virtqueue *txvq = txq->vtntx_vq;
360 
361 		if (state) {
362 			vtnet_rxq_enable_intr(rxq);
363 			virtqueue_enable_intr(txvq);
364 		} else {
365 			vtnet_rxq_disable_intr(rxq);
366 			virtqueue_disable_intr(txvq);
367 		}
368 	}
369 }
370 
371 static int
372 vtnet_netmap_tx_slots(struct vtnet_softc *sc)
373 {
374 	int div;
375 
376 	/* We need to prepend a virtio-net header to each netmap buffer to be
377 	 * transmitted, therefore calling virtqueue_enqueue() passing sglist
378 	 * with 2 elements.
379 	 * TX virtqueues use indirect descriptors if the feature was negotiated
380 	 * with the host, and if sc->vtnet_tx_nsegs > 1. With indirect
381 	 * descriptors, a single virtio descriptor is sufficient to reference
382 	 * each TX sglist. Without them, we need two separate virtio descriptors
383 	 * for each TX sglist. We therefore compute the number of netmap TX
384 	 * slots according to these assumptions.
385 	 */
386 	if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) && sc->vtnet_tx_nsegs > 1)
387 		div = 1;
388 	else
389 		div = 2;
390 
391 	return virtqueue_size(sc->vtnet_txqs[0].vtntx_vq) / div;
392 }
393 
394 static int
395 vtnet_netmap_rx_slots(struct vtnet_softc *sc)
396 {
397 	int div;
398 
399 	/* We need to prepend a virtio-net header to each netmap buffer to be
400 	 * received, therefore calling virtqueue_enqueue() passing sglist
401 	 * with 2 elements.
402 	 * RX virtqueues use indirect descriptors if the feature was negotiated
403 	 * with the host, and if sc->vtnet_rx_nsegs > 1. With indirect
404 	 * descriptors, a single virtio descriptor is sufficient to reference
405 	 * each RX sglist. Without them, we need two separate virtio descriptors
406 	 * for each RX sglist. We therefore compute the number of netmap RX
407 	 * slots according to these assumptions.
408 	 */
409 	if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) && sc->vtnet_rx_nsegs > 1)
410 		div = 1;
411 	else
412 		div = 2;
413 
414 	return virtqueue_size(sc->vtnet_rxqs[0].vtnrx_vq) / div;
415 }
416 
417 static int
418 vtnet_netmap_config(struct netmap_adapter *na, struct nm_config_info *info)
419 {
420 	struct vtnet_softc *sc = na->ifp->if_softc;
421 
422 	info->num_tx_rings = sc->vtnet_act_vq_pairs;
423 	info->num_rx_rings = sc->vtnet_act_vq_pairs;
424 	info->num_tx_descs = vtnet_netmap_tx_slots(sc);
425 	info->num_rx_descs = vtnet_netmap_rx_slots(sc);
426 	info->rx_buf_maxsize = NETMAP_BUF_SIZE(na);
427 
428 	return 0;
429 }
430 
431 static void
432 vtnet_netmap_attach(struct vtnet_softc *sc)
433 {
434 	struct netmap_adapter na;
435 
436 	bzero(&na, sizeof(na));
437 
438 	na.ifp = sc->vtnet_ifp;
439 	na.na_flags = NAF_OFFSETS;
440 	na.num_tx_desc = vtnet_netmap_tx_slots(sc);
441 	na.num_rx_desc = vtnet_netmap_rx_slots(sc);
442 	na.num_tx_rings = na.num_rx_rings = sc->vtnet_max_vq_pairs;
443 	na.rx_buf_maxsize = 0;
444 	na.nm_register = vtnet_netmap_reg;
445 	na.nm_txsync = vtnet_netmap_txsync;
446 	na.nm_rxsync = vtnet_netmap_rxsync;
447 	na.nm_intr = vtnet_netmap_intr;
448 	na.nm_config = vtnet_netmap_config;
449 
450 	netmap_attach(&na);
451 
452 	nm_prinf("vtnet attached txq=%d, txd=%d rxq=%d, rxd=%d",
453 			na.num_tx_rings, na.num_tx_desc,
454 			na.num_tx_rings, na.num_rx_desc);
455 }
456 /* end of file */
457