xref: /freebsd/sys/net/debugnet.c (revision 8270d35eca6381a18665c24a61a11d5914ee59fb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Isilon Systems, LLC.
5  * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved.
6  * Copyright (c) 2000 Darrell Anderson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_ddb.h"
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #include <ddb/db_lex.h>
47 #endif
48 
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_options.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64 
65 #include <machine/in_cksum.h>
66 #include <machine/pcb.h>
67 
68 #include <net/debugnet.h>
69 #define	DEBUGNET_INTERNAL
70 #include <net/debugnet_int.h>
71 
72 FEATURE(debugnet, "Debugnet support");
73 
74 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD, NULL,
75     "debugnet parameters");
76 
77 unsigned debugnet_debug;
78 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
79     &debugnet_debug, 0,
80     "Debug message verbosity (0: off; 1: on; 2: verbose)");
81 
82 int debugnet_npolls = 2000;
83 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
84     &debugnet_npolls, 0,
85     "Number of times to poll before assuming packet loss (0.5ms per poll)");
86 int debugnet_nretries = 10;
87 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
88     &debugnet_nretries, 0,
89     "Number of retransmit attempts before giving up");
90 
91 static bool g_debugnet_pcb_inuse;
92 static struct debugnet_pcb g_dnet_pcb;
93 
94 /*
95  * Simple accessors for opaque PCB.
96  */
97 const unsigned char *
98 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
99 {
100 	MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
101 	    pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
102 	return (pcb->dp_gw_mac.octet);
103 }
104 
105 /*
106  * Start of network primitives, beginning with output primitives.
107  */
108 
109 /*
110  * Handles creation of the ethernet header, then places outgoing packets into
111  * the tx buffer for the NIC
112  *
113  * Parameters:
114  *	m	The mbuf containing the packet to be sent (will be freed by
115  *		this function or the NIC driver)
116  *	ifp	The interface to send on
117  *	dst	The destination ethernet address (source address will be looked
118  *		up using ifp)
119  *	etype	The ETHERTYPE_* value for the protocol that is being sent
120  *
121  * Returns:
122  *	int	see errno.h, 0 for success
123  */
124 int
125 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
126     u_short etype)
127 {
128 	struct ether_header *eh;
129 
130 	if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
131 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
132 		if_printf(ifp, "%s: interface isn't up\n", __func__);
133 		m_freem(m);
134 		return (ENETDOWN);
135 	}
136 
137 	/* Fill in the ethernet header. */
138 	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
139 	if (m == NULL) {
140 		printf("%s: out of mbufs\n", __func__);
141 		return (ENOBUFS);
142 	}
143 	eh = mtod(m, struct ether_header *);
144 	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
145 	memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
146 	eh->ether_type = htons(etype);
147 	return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
148 }
149 
150 /*
151  * Unreliable transmission of an mbuf chain to the debugnet server
152  * Note: can't handle fragmentation; fails if the packet is larger than
153  *	 ifp->if_mtu after adding the UDP/IP headers
154  *
155  * Parameters:
156  *	pcb	The debugnet context block
157  *	m	mbuf chain
158  *
159  * Returns:
160  *	int	see errno.h, 0 for success
161  */
162 static int
163 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
164 {
165 	struct udphdr *udp;
166 
167 	MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
168 
169 	M_PREPEND(m, sizeof(*udp), M_NOWAIT);
170 	if (m == NULL) {
171 		printf("%s: out of mbufs\n", __func__);
172 		return (ENOBUFS);
173 	}
174 
175 	udp = mtod(m, void *);
176 	udp->uh_ulen = htons(m->m_pkthdr.len);
177 	/* Use this src port so that the server can connect() the socket */
178 	udp->uh_sport = htons(pcb->dp_client_ack_port);
179 	udp->uh_dport = htons(pcb->dp_server_port);
180 	/* Computed later (protocol-dependent). */
181 	udp->uh_sum = 0;
182 
183 	return (debugnet_ip_output(pcb, m));
184 }
185 
186 /*
187  * Dummy free function for debugnet clusters.
188  */
189 static void
190 debugnet_mbuf_free(struct mbuf *m __unused)
191 {
192 }
193 
194 /*
195  * Construct and reliably send a debugnet packet.  May fail from a resource
196  * shortage or extreme number of unacknowledged retransmissions.  Wait for
197  * an acknowledgement before returning.  Splits packets into chunks small
198  * enough to be sent without fragmentation (looks up the interface MTU)
199  *
200  * Parameters:
201  *	type	debugnet packet type (HERALD, FINISHED, ...)
202  *	data	data
203  *	datalen	data size (bytes)
204  *	auxdata	optional auxiliary information
205  *
206  * Returns:
207  *	int see errno.h, 0 for success
208  */
209 int
210 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
211     uint32_t datalen, const struct debugnet_proto_aux *auxdata)
212 {
213 	struct debugnet_msg_hdr *dn_msg_hdr;
214 	struct mbuf *m, *m2;
215 	uint64_t want_acks;
216 	uint32_t i, pktlen, sent_so_far;
217 	int retries, polls, error;
218 
219 	want_acks = 0;
220 	pcb->dp_rcvd_acks = 0;
221 	retries = 0;
222 
223 retransmit:
224 	/* Chunks can be too big to fit in packets. */
225 	for (i = sent_so_far = 0; sent_so_far < datalen ||
226 	    (i == 0 && datalen == 0); i++) {
227 		pktlen = datalen - sent_so_far;
228 
229 		/* Bound: the interface MTU (assume no IP options). */
230 		pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
231 		    sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
232 
233 		/*
234 		 * Check if it is retransmitting and this has been ACKed
235 		 * already.
236 		 */
237 		if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
238 			sent_so_far += pktlen;
239 			continue;
240 		}
241 
242 		/*
243 		 * Get and fill a header mbuf, then chain data as an extended
244 		 * mbuf.
245 		 */
246 		m = m_gethdr(M_NOWAIT, MT_DATA);
247 		if (m == NULL) {
248 			printf("%s: Out of mbufs\n", __func__);
249 			return (ENOBUFS);
250 		}
251 		m->m_len = sizeof(struct debugnet_msg_hdr);
252 		m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
253 		MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
254 		dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
255 		dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
256 		dn_msg_hdr->mh_type = htonl(type);
257 		dn_msg_hdr->mh_len = htonl(pktlen);
258 
259 		if (auxdata != NULL) {
260 			dn_msg_hdr->mh_offset =
261 			    htobe64(auxdata->dp_offset_start + sent_so_far);
262 			dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
263 		} else {
264 			dn_msg_hdr->mh_offset = htobe64(sent_so_far);
265 			dn_msg_hdr->mh_aux2 = 0;
266 		}
267 
268 		if (pktlen != 0) {
269 			m2 = m_get(M_NOWAIT, MT_DATA);
270 			if (m2 == NULL) {
271 				m_freem(m);
272 				printf("%s: Out of mbufs\n", __func__);
273 				return (ENOBUFS);
274 			}
275 			MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
276 			    pktlen, debugnet_mbuf_free, NULL, NULL, 0,
277 			    EXT_DISPOSABLE);
278 			m2->m_len = pktlen;
279 
280 			m_cat(m, m2);
281 			m->m_pkthdr.len += pktlen;
282 		}
283 		error = debugnet_udp_output(pcb, m);
284 		if (error != 0)
285 			return (error);
286 
287 		/* Note that we're waiting for this packet in the bitfield. */
288 		want_acks |= (1 << i);
289 		sent_so_far += pktlen;
290 	}
291 	if (i >= DEBUGNET_MAX_IN_FLIGHT)
292 		printf("Warning: Sent more than %d packets (%d). "
293 		    "Acknowledgements will fail unless the size of "
294 		    "rcvd_acks/want_acks is increased.\n",
295 		    DEBUGNET_MAX_IN_FLIGHT, i);
296 
297 	/*
298 	 * Wait for acks.  A *real* window would speed things up considerably.
299 	 */
300 	polls = 0;
301 	while (pcb->dp_rcvd_acks != want_acks) {
302 		if (polls++ > debugnet_npolls) {
303 			if (retries++ > debugnet_nretries)
304 				return (ETIMEDOUT);
305 			printf(". ");
306 			goto retransmit;
307 		}
308 		debugnet_network_poll(pcb->dp_ifp);
309 		DELAY(500);
310 	}
311 	pcb->dp_seqno += i;
312 	return (0);
313 }
314 
315 /*
316  * Network input primitives.
317  */
318 
319 static void
320 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
321 {
322 	const struct debugnet_ack *dn_ack;
323 	struct mbuf *m;
324 	uint32_t rcv_ackno;
325 
326 	m = *mb;
327 
328 	if (m->m_pkthdr.len < sizeof(*dn_ack)) {
329 		DNETDEBUG("ignoring small ACK packet\n");
330 		return;
331 	}
332 	/* Get Ack. */
333 	if (m->m_len < sizeof(*dn_ack)) {
334 		m = m_pullup(m, sizeof(*dn_ack));
335 		*mb = m;
336 		if (m == NULL) {
337 			DNETDEBUG("m_pullup failed\n");
338 			return;
339 		}
340 	}
341 	dn_ack = mtod(m, const void *);
342 
343 	/* Debugnet processing. */
344 	/*
345 	 * Packet is meant for us.  Extract the ack sequence number and the
346 	 * port number if necessary.
347 	 */
348 	rcv_ackno = ntohl(dn_ack->da_seqno);
349 	if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
350 		pcb->dp_server_port = sport;
351 		pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
352 	}
353 	if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
354 		printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
355 	else if (rcv_ackno >= pcb->dp_seqno) {
356 		/* We're interested in this ack. Record it. */
357 		pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
358 	}
359 }
360 
361 void
362 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
363 {
364 	const struct udphdr *udp;
365 	struct mbuf *m;
366 	uint16_t sport;
367 
368 	/* UDP processing. */
369 
370 	m = *mb;
371 	if (m->m_pkthdr.len < sizeof(*udp)) {
372 		DNETDEBUG("ignoring small UDP packet\n");
373 		return;
374 	}
375 
376 	/* Get UDP headers. */
377 	if (m->m_len < sizeof(*udp)) {
378 		m = m_pullup(m, sizeof(*udp));
379 		*mb = m;
380 		if (m == NULL) {
381 			DNETDEBUG("m_pullup failed\n");
382 			return;
383 		}
384 	}
385 	udp = mtod(m, const void *);
386 
387 	/* For now, the only UDP packets we expect to receive are acks. */
388 	if (ntohs(udp->uh_dport) != pcb->dp_client_ack_port) {
389 		DNETDEBUG("not on the expected ACK port.\n");
390 		return;
391 	}
392 	sport = ntohs(udp->uh_sport);
393 
394 	m_adj(m, sizeof(*udp));
395 	debugnet_handle_ack(pcb, mb, sport);
396 }
397 
398 /*
399  * Handler for incoming packets directly from the network adapter
400  * Identifies the packet type (IP or ARP) and passes it along to one of the
401  * helper functions debugnet_handle_ip or debugnet_handle_arp.
402  *
403  * It needs to partially replicate the behaviour of ether_input() and
404  * ether_demux().
405  *
406  * Parameters:
407  *	ifp	the interface the packet came from
408  *	m	an mbuf containing the packet received
409  */
410 static void
411 debugnet_pkt_in(struct ifnet *ifp, struct mbuf *m)
412 {
413 	struct ifreq ifr;
414 	struct ether_header *eh;
415 	u_short etype;
416 
417 	/* Ethernet processing. */
418 	if ((m->m_flags & M_PKTHDR) == 0) {
419 		DNETDEBUG_IF(ifp, "discard frame without packet header\n");
420 		goto done;
421 	}
422 	if (m->m_len < ETHER_HDR_LEN) {
423 		DNETDEBUG_IF(ifp,
424 	    "discard frame without leading eth header (len %u pktlen %u)\n",
425 		    m->m_len, m->m_pkthdr.len);
426 		goto done;
427 	}
428 	if ((m->m_flags & M_HASFCS) != 0) {
429 		m_adj(m, -ETHER_CRC_LEN);
430 		m->m_flags &= ~M_HASFCS;
431 	}
432 	eh = mtod(m, struct ether_header *);
433 	etype = ntohs(eh->ether_type);
434 	if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
435 		DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
436 		goto done;
437 	}
438 	if (if_gethwaddr(ifp, &ifr) != 0) {
439 		DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
440 		goto done;
441 	}
442 	if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
443 	    ETHER_ADDR_LEN) != 0 &&
444 	    (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
445 		DNETDEBUG_IF(ifp,
446 		    "discard frame with incorrect destination addr\n");
447 		goto done;
448 	}
449 
450 	MPASS(g_debugnet_pcb_inuse);
451 
452 	/* Done ethernet processing. Strip off the ethernet header. */
453 	m_adj(m, ETHER_HDR_LEN);
454 	switch (etype) {
455 	case ETHERTYPE_ARP:
456 		debugnet_handle_arp(&g_dnet_pcb, &m);
457 		break;
458 	case ETHERTYPE_IP:
459 		debugnet_handle_ip(&g_dnet_pcb, &m);
460 		break;
461 	default:
462 		DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
463 		break;
464 	}
465 done:
466 	if (m != NULL)
467 		m_freem(m);
468 }
469 
470 /*
471  * Network polling primitive.
472  *
473  * Instead of assuming that most of the network stack is sane, we just poll the
474  * driver directly for packets.
475  */
476 void
477 debugnet_network_poll(struct ifnet *ifp)
478 {
479 	ifp->if_debugnet_methods->dn_poll(ifp, 1000);
480 }
481 
482 /*
483  * Start of consumer API surface.
484  */
485 void
486 debugnet_free(struct debugnet_pcb *pcb)
487 {
488 	struct ifnet *ifp;
489 
490 	MPASS(g_debugnet_pcb_inuse);
491 	MPASS(pcb == &g_dnet_pcb);
492 
493 	ifp = pcb->dp_ifp;
494 	ifp->if_input = pcb->dp_drv_input;
495 	ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
496 	debugnet_mbuf_finish();
497 
498 	g_debugnet_pcb_inuse = false;
499 	memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
500 }
501 
502 int
503 debugnet_connect(const struct debugnet_conn_params *dcp,
504     struct debugnet_pcb **pcb_out)
505 {
506 	struct debugnet_pcb *pcb;
507 	struct ifnet *ifp;
508 	int error;
509 
510 	if (g_debugnet_pcb_inuse) {
511 		printf("%s: Only one connection at a time.\n", __func__);
512 		return (EBUSY);
513 	}
514 
515 	pcb = &g_dnet_pcb;
516 	*pcb = (struct debugnet_pcb) {
517 		.dp_state = DN_STATE_INIT,
518 		.dp_client = dcp->dc_client,
519 		.dp_server = dcp->dc_server,
520 		.dp_gateway = dcp->dc_gateway,
521 		.dp_server_port = dcp->dc_herald_port,	/* Initially */
522 		.dp_client_ack_port = dcp->dc_client_ack_port,
523 		.dp_seqno = 1,
524 		.dp_ifp = dcp->dc_ifp,
525 	};
526 
527 	/* Switch to the debugnet mbuf zones. */
528 	debugnet_mbuf_start();
529 
530 	ifp = pcb->dp_ifp;
531 	ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
532 
533 	/*
534 	 * We maintain the invariant that g_debugnet_pcb_inuse is always true
535 	 * while the debugnet ifp's if_input is overridden with
536 	 * debugnet_pkt_in.
537 	 */
538 	g_debugnet_pcb_inuse = true;
539 
540 	/* Make the card use *our* receive callback. */
541 	pcb->dp_drv_input = ifp->if_input;
542 	ifp->if_input = debugnet_pkt_in;
543 
544 	printf("%s: searching for %s MAC...\n", __func__,
545 	    (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
546 
547 	error = debugnet_arp_gw(pcb);
548 	if (error != 0) {
549 		printf("%s: failed to locate MAC address\n", __func__);
550 		goto cleanup;
551 	}
552 	MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
553 
554 	error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
555 	    dcp->dc_herald_datalen, NULL);
556 	if (error != 0) {
557 		printf("%s: failed to herald debugnet server\n", __func__);
558 		goto cleanup;
559 	}
560 
561 	*pcb_out = pcb;
562 	return (0);
563 
564 cleanup:
565 	debugnet_free(pcb);
566 	return (error);
567 }
568 
569 /*
570  * Pre-allocated dump-time mbuf tracking.
571  *
572  * We just track the high water mark we've ever seen and allocate appropriately
573  * for that iface/mtu combo.
574  */
575 static struct {
576 	int nmbuf;
577 	int ncl;
578 	int clsize;
579 } dn_hwm;
580 static struct mtx dn_hwm_lk;
581 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
582 
583 static void
584 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
585 {
586 	bool any;
587 
588 	any = false;
589 	mtx_lock(&dn_hwm_lk);
590 
591 	if (nmbuf > dn_hwm.nmbuf) {
592 		any = true;
593 		dn_hwm.nmbuf = nmbuf;
594 	} else
595 		nmbuf = dn_hwm.nmbuf;
596 
597 	if (ncl > dn_hwm.ncl) {
598 		any = true;
599 		dn_hwm.ncl = ncl;
600 	} else
601 		ncl = dn_hwm.ncl;
602 
603 	if (clsize > dn_hwm.clsize) {
604 		any = true;
605 		dn_hwm.clsize = clsize;
606 	} else
607 		clsize = dn_hwm.clsize;
608 
609 	mtx_unlock(&dn_hwm_lk);
610 
611 	if (any)
612 		debugnet_mbuf_reinit(nmbuf, ncl, clsize);
613 }
614 
615 void
616 debugnet_any_ifnet_update(struct ifnet *ifp)
617 {
618 	int clsize, nmbuf, ncl, nrxr;
619 
620 	if (!DEBUGNET_SUPPORTED_NIC(ifp))
621 		return;
622 
623 	ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
624 	KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
625 
626 	/*
627 	 * We need two headers per message on the transmit side. Multiply by
628 	 * four to give us some breathing room.
629 	 */
630 	nmbuf = ncl * (4 + nrxr);
631 	ncl *= nrxr;
632 
633 	dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
634 }
635 
636 /*
637  * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
638  * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
639  *
640  * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
641  * because the driver is still in attach.  Since we cannot use down interfaces,
642  * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient?  ... Nope, at least
643  * with vtnet and dhcpclient that event just never occurs.
644  *
645  * So that's how I've landed on the lower level ifnet_link_event.
646  */
647 
648 static void
649 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
650 {
651 	if (link_state == LINK_STATE_UP)
652 		debugnet_any_ifnet_update(ifp);
653 }
654 
655 static eventhandler_tag dn_attach_cookie;
656 static void
657 dn_evh_init(void *ctx __unused)
658 {
659 	dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
660 	    dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
661 }
662 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
663 
664 /*
665  * DDB parsing helpers for debugnet(4) consumers.
666  */
667 #ifdef DDB
668 struct my_inet_opt {
669 	bool has_opt;
670 	const char *printname;
671 	in_addr_t *result;
672 };
673 
674 static int
675 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
676 {
677 	in_addr_t tmp;
678 	unsigned octet;
679 	int t;
680 
681 	tmp = 0;
682 	for (octet = 0; octet < 4; octet++) {
683 		t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
684 		if (t != tNUMBER) {
685 			db_printf("%s:%s: octet %u expected number; found %d\n",
686 			    __func__, opt->printname, octet, t);
687 			return (EINVAL);
688 		}
689 		/*
690 		 * db_lex lexes '-' distinctly from the number itself, but
691 		 * let's document that invariant.
692 		 */
693 		MPASS(db_tok_number >= 0);
694 
695 		if (db_tok_number > UINT8_MAX) {
696 			db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
697 			    opt->printname, octet, (intmax_t)db_tok_number);
698 			return (EDOM);
699 		}
700 
701 		/* Constructed host-endian and converted to network later. */
702 		tmp = (tmp << 8) | db_tok_number;
703 
704 		if (octet < 3) {
705 			t = db_read_token_flags(DRT_WSPACE);
706 			if (t != tDOT) {
707 				db_printf("%s:%s: octet %u expected '.'; found"
708 				    " %d\n", __func__, opt->printname, octet,
709 				    t);
710 				return (EINVAL);
711 			}
712 		}
713 	}
714 
715 	*opt->result = htonl(tmp);
716 	opt->has_opt = true;
717 	return (0);
718 }
719 
720 int
721 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
722 {
723 	struct ifnet *ifp;
724 	int t, error;
725 	bool want_ifp;
726 	char ch;
727 
728 	struct my_inet_opt opt_client = {
729 		.printname = "client",
730 		.result = &result->dd_client,
731 	},
732 	opt_server = {
733 		.printname = "server",
734 		.result = &result->dd_server,
735 	},
736 	opt_gateway = {
737 		.printname = "gateway",
738 		.result = &result->dd_gateway,
739 	},
740 	*cur_inet_opt;
741 
742 	ifp = NULL;
743 	memset(result, 0, sizeof(*result));
744 
745 	/*
746 	 * command [space] [-] [opt] [[space] [optarg]] ...
747 	 *
748 	 * db_command has already lexed 'command' for us.
749 	 */
750 	t = db_read_token_flags(DRT_WSPACE);
751 	if (t == tWSPACE)
752 		t = db_read_token_flags(DRT_WSPACE);
753 
754 	while (t != tEOL) {
755 		if (t != tMINUS) {
756 			db_printf("%s: Bad syntax; expected '-', got %d\n",
757 			    cmd, t);
758 			goto usage;
759 		}
760 
761 		t = db_read_token_flags(DRT_WSPACE);
762 		if (t != tIDENT) {
763 			db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
764 			    cmd, t);
765 			goto usage;
766 		}
767 
768 		if (strlen(db_tok_string) > 1) {
769 			db_printf("%s: Bad syntax; expected single option "
770 			    "flag, got '%s'\n", cmd, db_tok_string);
771 			goto usage;
772 		}
773 
774 		want_ifp = false;
775 		cur_inet_opt = NULL;
776 		switch ((ch = db_tok_string[0])) {
777 		default:
778 			DNETDEBUG("Unexpected: '%c'\n", ch);
779 			/* FALLTHROUGH */
780 		case 'h':
781 			goto usage;
782 		case 'c':
783 			cur_inet_opt = &opt_client;
784 			break;
785 		case 'g':
786 			cur_inet_opt = &opt_gateway;
787 			break;
788 		case 's':
789 			cur_inet_opt = &opt_server;
790 			break;
791 		case 'i':
792 			want_ifp = true;
793 			break;
794 		}
795 
796 		t = db_read_token_flags(DRT_WSPACE);
797 		if (t != tWSPACE) {
798 			db_printf("%s: Bad syntax; expected space after "
799 			    "flag %c, got %d\n", cmd, ch, t);
800 			goto usage;
801 		}
802 
803 		if (want_ifp) {
804 			t = db_read_token_flags(DRT_WSPACE);
805 			if (t != tIDENT) {
806 				db_printf("%s: Expected interface but got %d\n",
807 				    cmd, t);
808 				goto usage;
809 			}
810 
811 			CURVNET_SET(vnet0);
812 			/*
813 			 * We *don't* take a ref here because the only current
814 			 * consumer, db_netdump_cmd, does not need it.  It
815 			 * (somewhat redundantly) extracts the if_name(),
816 			 * re-lookups the ifp, and takes its own reference.
817 			 */
818 			ifp = ifunit(db_tok_string);
819 			CURVNET_RESTORE();
820 			if (ifp == NULL) {
821 				db_printf("Could not locate interface %s\n",
822 				    db_tok_string);
823 				goto cleanup;
824 			}
825 		} else {
826 			MPASS(cur_inet_opt != NULL);
827 			/* Assume IPv4 for now. */
828 			error = dn_parse_optarg_ipv4(cur_inet_opt);
829 			if (error != 0)
830 				goto cleanup;
831 		}
832 
833 		/* Skip (mandatory) whitespace after option, if not EOL. */
834 		t = db_read_token_flags(DRT_WSPACE);
835 		if (t == tEOL)
836 			break;
837 		if (t != tWSPACE) {
838 			db_printf("%s: Bad syntax; expected space after "
839 			    "flag %c option; got %d\n", cmd, ch, t);
840 			goto usage;
841 		}
842 		t = db_read_token_flags(DRT_WSPACE);
843 	}
844 
845 	/* Currently, all three are required. */
846 	if (!opt_client.has_opt || !opt_server.has_opt || ifp == NULL) {
847 		db_printf("%s needs all of client, server, and interface "
848 		    "specified.\n", cmd);
849 		goto usage;
850 	}
851 
852 	result->dd_has_gateway = opt_gateway.has_opt;
853 
854 	/* Iface validation stolen from netdump_configure. */
855 	if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
856 		db_printf("%s: interface '%s' does not support debugnet\n",
857 		    cmd, if_name(ifp));
858 		error = ENODEV;
859 		goto cleanup;
860 	}
861 	if ((if_getflags(ifp) & IFF_UP) == 0) {
862 		db_printf("%s: interface '%s' link is down\n", cmd,
863 		    if_name(ifp));
864 		error = ENXIO;
865 		goto cleanup;
866 	}
867 
868 	result->dd_ifp = ifp;
869 
870 	/* We parsed the full line to tEOL already, or bailed with an error. */
871 	return (0);
872 
873 usage:
874 	db_printf("Usage: %s -s <server> [-g <gateway>] -c <localip> "
875 	    "-i <interface>\n", cmd);
876 	error = EINVAL;
877 	/* FALLTHROUGH */
878 cleanup:
879 	db_skip_to_eol();
880 	return (error);
881 }
882 #endif /* DDB */
883