2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Isilon Systems, LLC.
5 * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved.
6 * Copyright (c) 2000 Darrell Anderson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/eventhandler.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
47 #include <ddb/db_lex.h>
50 #include <net/ethernet.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 #include <net/if_var.h>
56 #include <net/route.h>
57 #include <net/route/nhop.h>
59 #include <netinet/in.h>
60 #include <netinet/in_fib.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_options.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
69 #include <machine/in_cksum.h>
70 #include <machine/pcb.h>
72 #include <net/debugnet.h>
73 #define DEBUGNET_INTERNAL
74 #include <net/debugnet_int.h>
76 FEATURE(debugnet, "Debugnet support");
78 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
79 "debugnet parameters");
81 unsigned debugnet_debug;
82 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
84 "Debug message verbosity (0: off; 1: on; 2: verbose)");
86 int debugnet_npolls = 2000;
87 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
89 "Number of times to poll before assuming packet loss (0.5ms per poll)");
90 int debugnet_nretries = 10;
91 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
92 &debugnet_nretries, 0,
93 "Number of retransmit attempts before giving up");
95 static bool g_debugnet_pcb_inuse;
96 static struct debugnet_pcb g_dnet_pcb;
99 * Simple accessors for opaque PCB.
101 const unsigned char *
102 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
104 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
105 pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
106 return (pcb->dp_gw_mac.octet);
110 * Start of network primitives, beginning with output primitives.
114 * Handles creation of the ethernet header, then places outgoing packets into
115 * the tx buffer for the NIC
118 * m The mbuf containing the packet to be sent (will be freed by
119 * this function or the NIC driver)
120 * ifp The interface to send on
121 * dst The destination ethernet address (source address will be looked
123 * etype The ETHERTYPE_* value for the protocol that is being sent
126 * int see errno.h, 0 for success
129 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
132 struct ether_header *eh;
134 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
135 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
136 if_printf(ifp, "%s: interface isn't up\n", __func__);
141 /* Fill in the ethernet header. */
142 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
144 printf("%s: out of mbufs\n", __func__);
147 eh = mtod(m, struct ether_header *);
148 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
149 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
150 eh->ether_type = htons(etype);
151 return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
155 * Unreliable transmission of an mbuf chain to the debugnet server
156 * Note: can't handle fragmentation; fails if the packet is larger than
157 * ifp->if_mtu after adding the UDP/IP headers
160 * pcb The debugnet context block
164 * int see errno.h, 0 for success
167 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
171 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
173 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
175 printf("%s: out of mbufs\n", __func__);
179 udp = mtod(m, void *);
180 udp->uh_ulen = htons(m->m_pkthdr.len);
181 /* Use this src port so that the server can connect() the socket */
182 udp->uh_sport = htons(pcb->dp_client_port);
183 udp->uh_dport = htons(pcb->dp_server_port);
184 /* Computed later (protocol-dependent). */
187 return (debugnet_ip_output(pcb, m));
191 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
193 struct debugnet_ack *dn_ack;
196 DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
198 m = m_gethdr(M_NOWAIT, MT_DATA);
200 printf("%s: Out of mbufs\n", __func__);
203 m->m_len = sizeof(*dn_ack);
204 m->m_pkthdr.len = sizeof(*dn_ack);
205 MH_ALIGN(m, sizeof(*dn_ack));
206 dn_ack = mtod(m, void *);
207 dn_ack->da_seqno = seqno;
209 return (debugnet_udp_output(pcb, m));
213 * Dummy free function for debugnet clusters.
216 debugnet_mbuf_free(struct mbuf *m __unused)
221 * Construct and reliably send a debugnet packet. May fail from a resource
222 * shortage or extreme number of unacknowledged retransmissions. Wait for
223 * an acknowledgement before returning. Splits packets into chunks small
224 * enough to be sent without fragmentation (looks up the interface MTU)
227 * type debugnet packet type (HERALD, FINISHED, ...)
229 * datalen data size (bytes)
230 * auxdata optional auxiliary information
233 * int see errno.h, 0 for success
236 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
237 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
239 struct debugnet_msg_hdr *dn_msg_hdr;
242 uint32_t i, pktlen, sent_so_far;
243 int retries, polls, error;
245 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
249 pcb->dp_rcvd_acks = 0;
253 /* Chunks can be too big to fit in packets. */
254 for (i = sent_so_far = 0; sent_so_far < datalen ||
255 (i == 0 && datalen == 0); i++) {
256 pktlen = datalen - sent_so_far;
258 /* Bound: the interface MTU (assume no IP options). */
259 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
260 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
263 * Check if it is retransmitting and this has been ACKed
266 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
267 sent_so_far += pktlen;
272 * Get and fill a header mbuf, then chain data as an extended
275 m = m_gethdr(M_NOWAIT, MT_DATA);
277 printf("%s: Out of mbufs\n", __func__);
280 m->m_len = sizeof(struct debugnet_msg_hdr);
281 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
282 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
283 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
284 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
285 dn_msg_hdr->mh_type = htonl(type);
286 dn_msg_hdr->mh_len = htonl(pktlen);
288 if (auxdata != NULL) {
289 dn_msg_hdr->mh_offset =
290 htobe64(auxdata->dp_offset_start + sent_so_far);
291 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
293 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
294 dn_msg_hdr->mh_aux2 = 0;
298 m2 = m_get(M_NOWAIT, MT_DATA);
301 printf("%s: Out of mbufs\n", __func__);
304 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
305 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
310 m->m_pkthdr.len += pktlen;
312 error = debugnet_udp_output(pcb, m);
316 /* Note that we're waiting for this packet in the bitfield. */
317 want_acks |= (1 << i);
318 sent_so_far += pktlen;
320 if (i >= DEBUGNET_MAX_IN_FLIGHT)
321 printf("Warning: Sent more than %d packets (%d). "
322 "Acknowledgements will fail unless the size of "
323 "rcvd_acks/want_acks is increased.\n",
324 DEBUGNET_MAX_IN_FLIGHT, i);
327 * Wait for acks. A *real* window would speed things up considerably.
330 while (pcb->dp_rcvd_acks != want_acks) {
331 if (polls++ > debugnet_npolls) {
332 if (retries++ > debugnet_nretries)
337 debugnet_network_poll(pcb);
339 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
347 * Network input primitives.
351 * Just introspect the header enough to fire off a seqno ack and validate
355 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
357 const struct debugnet_msg_hdr *dnh;
363 if (m->m_pkthdr.len < sizeof(*dnh)) {
364 DNETDEBUG("ignoring small debugnet_msg packet\n");
369 if (m->m_len < sizeof(*dnh)) {
370 m = m_pullup(m, sizeof(*dnh));
373 DNETDEBUG("m_pullup failed\n");
377 dnh = mtod(m, const void *);
379 if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
380 DNETDEBUG("Dropping short packet.\n");
385 * If the issue is transient (ENOBUFS), sender should resend. If
386 * non-transient (like driver objecting to rx -> tx from the same
387 * thread), not much else we can do.
389 error = debugnet_ack_output(pcb, dnh->mh_seqno);
393 if (ntohl(dnh->mh_type) == DEBUGNET_FINISHED) {
394 printf("Remote shut down the connection on us!\n");
395 pcb->dp_state = DN_STATE_REMOTE_CLOSED;
398 * Continue through to the user handler so they are signalled
399 * not to wait for further rx.
403 pcb->dp_rx_handler(pcb, mb);
407 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
409 const struct debugnet_ack *dn_ack;
416 if (m->m_len < sizeof(*dn_ack)) {
417 m = m_pullup(m, sizeof(*dn_ack));
420 DNETDEBUG("m_pullup failed\n");
424 dn_ack = mtod(m, const void *);
426 /* Debugnet processing. */
428 * Packet is meant for us. Extract the ack sequence number and the
429 * port number if necessary.
431 rcv_ackno = ntohl(dn_ack->da_seqno);
432 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
433 pcb->dp_server_port = sport;
434 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
436 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
437 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
438 else if (rcv_ackno >= pcb->dp_seqno) {
439 /* We're interested in this ack. Record it. */
440 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
445 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
447 const struct udphdr *udp;
449 uint16_t sport, ulen;
451 /* UDP processing. */
454 if (m->m_pkthdr.len < sizeof(*udp)) {
455 DNETDEBUG("ignoring small UDP packet\n");
459 /* Get UDP headers. */
460 if (m->m_len < sizeof(*udp)) {
461 m = m_pullup(m, sizeof(*udp));
464 DNETDEBUG("m_pullup failed\n");
468 udp = mtod(m, const void *);
470 /* We expect to receive UDP packets on the configured client port. */
471 if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
472 DNETDEBUG("not on the expected port.\n");
476 /* Check that ulen does not exceed actual size of data. */
477 ulen = ntohs(udp->uh_ulen);
478 if (m->m_pkthdr.len < ulen) {
479 DNETDEBUG("ignoring runt UDP packet\n");
483 sport = ntohs(udp->uh_sport);
485 m_adj(m, sizeof(*udp));
486 ulen -= sizeof(*udp);
488 if (ulen == sizeof(struct debugnet_ack)) {
489 debugnet_handle_ack(pcb, mb, sport);
493 if (pcb->dp_rx_handler == NULL) {
494 if (ulen < sizeof(struct debugnet_ack))
495 DNETDEBUG("ignoring small ACK packet\n");
497 DNETDEBUG("ignoring unexpected non-ACK packet on "
498 "half-duplex connection.\n");
502 debugnet_handle_rx_msg(pcb, mb);
506 * Handler for incoming packets directly from the network adapter
507 * Identifies the packet type (IP or ARP) and passes it along to one of the
508 * helper functions debugnet_handle_ip or debugnet_handle_arp.
510 * It needs to partially replicate the behaviour of ether_input() and
514 * ifp the interface the packet came from
515 * m an mbuf containing the packet received
518 debugnet_pkt_in(struct ifnet *ifp, struct mbuf *m)
521 struct ether_header *eh;
524 /* Ethernet processing. */
525 if ((m->m_flags & M_PKTHDR) == 0) {
526 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
529 if (m->m_len < ETHER_HDR_LEN) {
531 "discard frame without leading eth header (len %u pktlen %u)\n",
532 m->m_len, m->m_pkthdr.len);
535 if ((m->m_flags & M_HASFCS) != 0) {
536 m_adj(m, -ETHER_CRC_LEN);
537 m->m_flags &= ~M_HASFCS;
539 eh = mtod(m, struct ether_header *);
540 etype = ntohs(eh->ether_type);
541 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
542 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
545 if (if_gethwaddr(ifp, &ifr) != 0) {
546 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
549 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
550 ETHER_ADDR_LEN) != 0 &&
551 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
553 "discard frame with incorrect destination addr\n");
557 MPASS(g_debugnet_pcb_inuse);
559 /* Done ethernet processing. Strip off the ethernet header. */
560 m_adj(m, ETHER_HDR_LEN);
563 debugnet_handle_arp(&g_dnet_pcb, &m);
566 debugnet_handle_ip(&g_dnet_pcb, &m);
569 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
578 * Network polling primitive.
580 * Instead of assuming that most of the network stack is sane, we just poll the
581 * driver directly for packets.
584 debugnet_network_poll(struct debugnet_pcb *pcb)
589 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
593 * Start of consumer API surface.
596 debugnet_free(struct debugnet_pcb *pcb)
600 MPASS(g_debugnet_pcb_inuse);
601 MPASS(pcb == &g_dnet_pcb);
605 if (pcb->dp_drv_input != NULL)
606 ifp->if_input = pcb->dp_drv_input;
607 if (pcb->dp_event_started)
608 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
610 debugnet_mbuf_finish();
612 g_debugnet_pcb_inuse = false;
613 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
617 debugnet_connect(const struct debugnet_conn_params *dcp,
618 struct debugnet_pcb **pcb_out)
620 struct debugnet_proto_aux herald_auxdata;
621 struct debugnet_pcb *pcb;
625 if (g_debugnet_pcb_inuse) {
626 printf("%s: Only one connection at a time.\n", __func__);
631 *pcb = (struct debugnet_pcb) {
632 .dp_state = DN_STATE_INIT,
633 .dp_client = dcp->dc_client,
634 .dp_server = dcp->dc_server,
635 .dp_gateway = dcp->dc_gateway,
636 .dp_server_port = dcp->dc_herald_port, /* Initially */
637 .dp_client_port = dcp->dc_client_port,
639 .dp_ifp = dcp->dc_ifp,
640 .dp_rx_handler = dcp->dc_rx_handler,
643 /* Switch to the debugnet mbuf zones. */
644 debugnet_mbuf_start();
646 /* At least one needed parameter is missing; infer it. */
647 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
648 pcb->dp_ifp == NULL) {
649 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
650 struct ifnet *rt_ifp;
651 struct nhop_object *nh;
653 memset(&dest_sin, 0, sizeof(dest_sin));
654 dest_sin = (struct sockaddr_in) {
655 .sin_len = sizeof(dest_sin),
656 .sin_family = AF_INET,
657 .sin_addr.s_addr = pcb->dp_server,
661 nh = fib4_lookup_debugnet(RT_DEFAULT_FIB, dest_sin.sin_addr, 0,
666 printf("%s: Could not get route for that server.\n",
672 if (nh->gw_sa.sa_family == AF_INET)
673 gw_sin = &nh->gw4_sa;
675 if (nh->gw_sa.sa_family == AF_LINK)
676 DNETDEBUG("Destination address is on link.\n");
680 MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
681 local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
685 if (pcb->dp_client == INADDR_ANY)
686 pcb->dp_client = local_sin->sin_addr.s_addr;
687 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
688 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
689 if (pcb->dp_ifp == NULL)
690 pcb->dp_ifp = rt_ifp;
695 if (debugnet_debug > 0) {
696 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
697 gwbuf[INET_ADDRSTRLEN];
698 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
699 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
700 if (pcb->dp_gateway != INADDR_ANY)
701 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
702 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
703 serbuf, pcb->dp_server_port,
704 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
705 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
706 clibuf, pcb->dp_client_port, if_name(ifp));
709 /* Validate iface is online and supported. */
710 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
711 printf("%s: interface '%s' does not support debugnet\n",
712 __func__, if_name(ifp));
716 if ((if_getflags(ifp) & IFF_UP) == 0) {
717 printf("%s: interface '%s' link is down\n", __func__,
723 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
724 pcb->dp_event_started = true;
727 * We maintain the invariant that g_debugnet_pcb_inuse is always true
728 * while the debugnet ifp's if_input is overridden with
731 g_debugnet_pcb_inuse = true;
733 /* Make the card use *our* receive callback. */
734 pcb->dp_drv_input = ifp->if_input;
735 ifp->if_input = debugnet_pkt_in;
737 printf("%s: searching for %s MAC...\n", __func__,
738 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
740 error = debugnet_arp_gw(pcb);
742 printf("%s: failed to locate MAC address\n", __func__);
745 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
747 herald_auxdata = (struct debugnet_proto_aux) {
748 .dp_offset_start = dcp->dc_herald_offset,
749 .dp_aux2 = dcp->dc_herald_aux2,
751 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
752 dcp->dc_herald_datalen, &herald_auxdata);
754 printf("%s: failed to herald debugnet server\n", __func__);
767 * Pre-allocated dump-time mbuf tracking.
769 * We just track the high water mark we've ever seen and allocate appropriately
770 * for that iface/mtu combo.
777 static struct mtx dn_hwm_lk;
778 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
781 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
786 mtx_lock(&dn_hwm_lk);
788 if (nmbuf > dn_hwm.nmbuf) {
790 dn_hwm.nmbuf = nmbuf;
792 nmbuf = dn_hwm.nmbuf;
794 if (ncl > dn_hwm.ncl) {
800 if (clsize > dn_hwm.clsize) {
802 dn_hwm.clsize = clsize;
804 clsize = dn_hwm.clsize;
806 mtx_unlock(&dn_hwm_lk);
809 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
813 debugnet_any_ifnet_update(struct ifnet *ifp)
815 int clsize, nmbuf, ncl, nrxr;
817 if (!DEBUGNET_SUPPORTED_NIC(ifp))
820 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
821 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
824 * We need two headers per message on the transmit side. Multiply by
825 * four to give us some breathing room.
827 nmbuf = ncl * (4 + nrxr);
831 * Bandaid for drivers that (incorrectly) advertise LinkUp before their
832 * dn_init method is available.
834 if (nmbuf == 0 || ncl == 0 || clsize == 0) {
835 printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
836 __func__, if_name(ifp), ifp);
839 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
843 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
844 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
846 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
847 * because the driver is still in attach. Since we cannot use down interfaces,
848 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
849 * with vtnet and dhcpclient that event just never occurs.
851 * So that's how I've landed on the lower level ifnet_link_event.
855 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
857 if (link_state == LINK_STATE_UP)
858 debugnet_any_ifnet_update(ifp);
861 static eventhandler_tag dn_attach_cookie;
863 dn_evh_init(void *ctx __unused)
865 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
866 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
868 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
871 * DDB parsing helpers for debugnet(4) consumers.
876 const char *printname;
881 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
888 for (octet = 0; octet < 4; octet++) {
889 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
891 db_printf("%s:%s: octet %u expected number; found %d\n",
892 __func__, opt->printname, octet, t);
896 * db_lex lexes '-' distinctly from the number itself, but
897 * let's document that invariant.
899 MPASS(db_tok_number >= 0);
901 if (db_tok_number > UINT8_MAX) {
902 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
903 opt->printname, octet, (intmax_t)db_tok_number);
907 /* Constructed host-endian and converted to network later. */
908 tmp = (tmp << 8) | db_tok_number;
911 t = db_read_token_flags(DRT_WSPACE);
913 db_printf("%s:%s: octet %u expected '.'; found"
914 " %d\n", __func__, opt->printname, octet,
921 *opt->result = htonl(tmp);
927 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
934 struct my_inet_opt opt_client = {
935 .printname = "client",
936 .result = &result->dd_client,
939 .printname = "server",
940 .result = &result->dd_server,
943 .printname = "gateway",
944 .result = &result->dd_gateway,
949 memset(result, 0, sizeof(*result));
952 * command [space] [-] [opt] [[space] [optarg]] ...
954 * db_command has already lexed 'command' for us.
956 t = db_read_token_flags(DRT_WSPACE);
958 t = db_read_token_flags(DRT_WSPACE);
962 db_printf("%s: Bad syntax; expected '-', got %d\n",
967 t = db_read_token_flags(DRT_WSPACE);
969 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
974 if (strlen(db_tok_string) > 1) {
975 db_printf("%s: Bad syntax; expected single option "
976 "flag, got '%s'\n", cmd, db_tok_string);
982 switch ((ch = db_tok_string[0])) {
984 DNETDEBUG("Unexpected: '%c'\n", ch);
989 cur_inet_opt = &opt_client;
992 cur_inet_opt = &opt_gateway;
995 cur_inet_opt = &opt_server;
1002 t = db_read_token_flags(DRT_WSPACE);
1004 db_printf("%s: Bad syntax; expected space after "
1005 "flag %c, got %d\n", cmd, ch, t);
1010 t = db_read_token_flags(DRT_WSPACE);
1012 db_printf("%s: Expected interface but got %d\n",
1019 * We *don't* take a ref here because the only current
1020 * consumer, db_netdump_cmd, does not need it. It
1021 * (somewhat redundantly) extracts the if_name(),
1022 * re-lookups the ifp, and takes its own reference.
1024 ifp = ifunit(db_tok_string);
1027 db_printf("Could not locate interface %s\n",
1032 MPASS(cur_inet_opt != NULL);
1033 /* Assume IPv4 for now. */
1034 error = dn_parse_optarg_ipv4(cur_inet_opt);
1039 /* Skip (mandatory) whitespace after option, if not EOL. */
1040 t = db_read_token_flags(DRT_WSPACE);
1044 db_printf("%s: Bad syntax; expected space after "
1045 "flag %c option; got %d\n", cmd, ch, t);
1048 t = db_read_token_flags(DRT_WSPACE);
1051 if (!opt_server.has_opt) {
1052 db_printf("%s: need a destination server address\n", cmd);
1056 result->dd_has_client = opt_client.has_opt;
1057 result->dd_has_gateway = opt_gateway.has_opt;
1058 result->dd_ifp = ifp;
1060 /* We parsed the full line to tEOL already, or bailed with an error. */
1064 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1065 "-i <interface>]\n", cmd);