2 * SPDX-License-Identifier: BSD-2-Clause
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>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/endian.h>
38 #include <sys/errno.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
42 #include <sys/mutex.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
48 #include <ddb/db_lex.h>
51 #include <net/ethernet.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/if_var.h>
57 #include <net/if_private.h>
59 #include <net/route.h>
60 #include <net/route/nhop.h>
62 #include <netinet/in.h>
63 #include <netinet/in_fib.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_options.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
72 #include <machine/in_cksum.h>
73 #include <machine/pcb.h>
75 #include <net/debugnet.h>
76 #define DEBUGNET_INTERNAL
77 #include <net/debugnet_int.h>
79 FEATURE(debugnet, "Debugnet support");
81 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
82 "debugnet parameters");
84 unsigned debugnet_debug;
85 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
87 "Debug message verbosity (0: off; 1: on; 2: verbose)");
89 int debugnet_npolls = 2000;
90 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
92 "Number of times to poll before assuming packet loss (0.5ms per poll)");
93 int debugnet_nretries = 10;
94 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
95 &debugnet_nretries, 0,
96 "Number of retransmit attempts before giving up");
97 int debugnet_fib = RT_DEFAULT_FIB;
98 SYSCTL_INT(_net_debugnet, OID_AUTO, fib, CTLFLAG_RWTUN,
100 "Fib to use when sending dump");
102 static bool g_debugnet_pcb_inuse;
103 static struct debugnet_pcb g_dnet_pcb;
106 * Simple accessors for opaque PCB.
108 const unsigned char *
109 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
111 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
112 pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
113 return (pcb->dp_gw_mac.octet);
117 debugnet_get_server_addr(const struct debugnet_pcb *pcb)
119 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
120 pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
121 return (&pcb->dp_server);
125 debugnet_get_server_port(const struct debugnet_pcb *pcb)
127 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
128 pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
129 return (pcb->dp_server_port);
133 * Start of network primitives, beginning with output primitives.
137 * Handles creation of the ethernet header, then places outgoing packets into
138 * the tx buffer for the NIC
141 * m The mbuf containing the packet to be sent (will be freed by
142 * this function or the NIC driver)
143 * ifp The interface to send on
144 * dst The destination ethernet address (source address will be looked
146 * etype The ETHERTYPE_* value for the protocol that is being sent
149 * int see errno.h, 0 for success
152 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
155 struct ether_header *eh;
157 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
158 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
159 if_printf(ifp, "%s: interface isn't up\n", __func__);
164 /* Fill in the ethernet header. */
165 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
167 printf("%s: out of mbufs\n", __func__);
170 eh = mtod(m, struct ether_header *);
171 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
172 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
173 eh->ether_type = htons(etype);
174 return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
178 * Unreliable transmission of an mbuf chain to the debugnet server
179 * Note: can't handle fragmentation; fails if the packet is larger than
180 * ifp->if_mtu after adding the UDP/IP headers
183 * pcb The debugnet context block
187 * int see errno.h, 0 for success
190 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
194 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
196 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
198 printf("%s: out of mbufs\n", __func__);
202 udp = mtod(m, void *);
203 udp->uh_ulen = htons(m->m_pkthdr.len);
204 /* Use this src port so that the server can connect() the socket */
205 udp->uh_sport = htons(pcb->dp_client_port);
206 udp->uh_dport = htons(pcb->dp_server_port);
207 /* Computed later (protocol-dependent). */
210 return (debugnet_ip_output(pcb, m));
214 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
216 struct debugnet_ack *dn_ack;
219 DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
221 m = m_gethdr(M_NOWAIT, MT_DATA);
223 printf("%s: Out of mbufs\n", __func__);
226 m->m_len = sizeof(*dn_ack);
227 m->m_pkthdr.len = sizeof(*dn_ack);
228 MH_ALIGN(m, sizeof(*dn_ack));
229 dn_ack = mtod(m, void *);
230 dn_ack->da_seqno = seqno;
232 return (debugnet_udp_output(pcb, m));
236 * Dummy free function for debugnet clusters.
239 debugnet_mbuf_free(struct mbuf *m __unused)
244 * Construct and reliably send a debugnet packet. May fail from a resource
245 * shortage or extreme number of unacknowledged retransmissions. Wait for
246 * an acknowledgement before returning. Splits packets into chunks small
247 * enough to be sent without fragmentation (looks up the interface MTU)
250 * type debugnet packet type (HERALD, FINISHED, ...)
252 * datalen data size (bytes)
253 * auxdata optional auxiliary information
256 * int see errno.h, 0 for success
259 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
260 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
262 struct debugnet_msg_hdr *dn_msg_hdr;
265 uint32_t i, pktlen, sent_so_far;
266 int retries, polls, error;
268 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
272 pcb->dp_rcvd_acks = 0;
276 /* Chunks can be too big to fit in packets. */
277 for (i = sent_so_far = 0; sent_so_far < datalen ||
278 (i == 0 && datalen == 0); i++) {
279 pktlen = datalen - sent_so_far;
281 /* Bound: the interface MTU (assume no IP options). */
282 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
283 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
286 * Check if it is retransmitting and this has been ACKed
289 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
290 sent_so_far += pktlen;
295 * Get and fill a header mbuf, then chain data as an extended
298 m = m_gethdr(M_NOWAIT, MT_DATA);
300 printf("%s: Out of mbufs\n", __func__);
303 m->m_len = sizeof(struct debugnet_msg_hdr);
304 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
305 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
306 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
307 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
308 dn_msg_hdr->mh_type = htonl(type);
309 dn_msg_hdr->mh_len = htonl(pktlen);
311 if (auxdata != NULL) {
312 dn_msg_hdr->mh_offset =
313 htobe64(auxdata->dp_offset_start + sent_so_far);
314 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
316 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
317 dn_msg_hdr->mh_aux2 = 0;
321 m2 = m_get(M_NOWAIT, MT_DATA);
324 printf("%s: Out of mbufs\n", __func__);
327 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
328 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
333 m->m_pkthdr.len += pktlen;
335 error = debugnet_udp_output(pcb, m);
339 /* Note that we're waiting for this packet in the bitfield. */
340 want_acks |= (1 << i);
341 sent_so_far += pktlen;
343 if (i >= DEBUGNET_MAX_IN_FLIGHT)
344 printf("Warning: Sent more than %d packets (%d). "
345 "Acknowledgements will fail unless the size of "
346 "rcvd_acks/want_acks is increased.\n",
347 DEBUGNET_MAX_IN_FLIGHT, i);
350 * Wait for acks. A *real* window would speed things up considerably.
353 while (pcb->dp_rcvd_acks != want_acks) {
354 if (polls++ > debugnet_npolls) {
355 if (retries++ > debugnet_nretries)
360 debugnet_network_poll(pcb);
362 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
370 * Network input primitives.
374 * Just introspect the header enough to fire off a seqno ack and validate
378 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
380 const struct debugnet_msg_hdr *dnh;
388 if (m->m_pkthdr.len < sizeof(*dnh)) {
389 DNETDEBUG("ignoring small debugnet_msg packet\n");
394 if (m->m_len < sizeof(*dnh)) {
395 m = m_pullup(m, sizeof(*dnh));
398 DNETDEBUG("m_pullup failed\n");
403 dnh = mtod(m, const void *);
404 if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
405 DNETDEBUG("Dropping short packet.\n");
409 hdr_type = ntohl(dnh->mh_type);
410 if (hdr_type != DEBUGNET_DATA) {
411 if (hdr_type == DEBUGNET_FINISHED) {
412 printf("Remote shut down the connection on us!\n");
413 pcb->dp_state = DN_STATE_REMOTE_CLOSED;
414 if (pcb->dp_finish_handler != NULL) {
415 pcb->dp_finish_handler();
418 DNETDEBUG("Got unexpected debugnet message %u\n", hdr_type);
424 * If the issue is transient (ENOBUFS), sender should resend. If
425 * non-transient (like driver objecting to rx -> tx from the same
426 * thread), not much else we can do.
428 seqno = dnh->mh_seqno; /* net endian */
429 m_adj(m, sizeof(*dnh));
431 error = pcb->dp_rx_handler(m);
433 DNETDEBUG("RX handler was not able to accept message, error %d. "
434 "Skipping ack.\n", error);
438 error = debugnet_ack_output(pcb, seqno);
440 DNETDEBUG("Couldn't ACK rx packet %u; %d\n", ntohl(seqno), error);
445 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
447 const struct debugnet_ack *dn_ack;
454 if (m->m_len < sizeof(*dn_ack)) {
455 m = m_pullup(m, sizeof(*dn_ack));
458 DNETDEBUG("m_pullup failed\n");
462 dn_ack = mtod(m, const void *);
464 /* Debugnet processing. */
466 * Packet is meant for us. Extract the ack sequence number and the
467 * port number if necessary.
469 rcv_ackno = ntohl(dn_ack->da_seqno);
470 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
471 pcb->dp_server_port = sport;
472 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
474 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
475 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
476 else if (rcv_ackno >= pcb->dp_seqno) {
477 /* We're interested in this ack. Record it. */
478 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
483 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
485 const struct udphdr *udp;
487 uint16_t sport, ulen;
489 /* UDP processing. */
492 if (m->m_pkthdr.len < sizeof(*udp)) {
493 DNETDEBUG("ignoring small UDP packet\n");
497 /* Get UDP headers. */
498 if (m->m_len < sizeof(*udp)) {
499 m = m_pullup(m, sizeof(*udp));
502 DNETDEBUG("m_pullup failed\n");
506 udp = mtod(m, const void *);
508 /* We expect to receive UDP packets on the configured client port. */
509 if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
510 DNETDEBUG("not on the expected port.\n");
514 /* Check that ulen does not exceed actual size of data. */
515 ulen = ntohs(udp->uh_ulen);
516 if (m->m_pkthdr.len < ulen) {
517 DNETDEBUG("ignoring runt UDP packet\n");
521 sport = ntohs(udp->uh_sport);
523 m_adj(m, sizeof(*udp));
524 ulen -= sizeof(*udp);
526 if (ulen == sizeof(struct debugnet_ack)) {
527 debugnet_handle_ack(pcb, mb, sport);
531 if (pcb->dp_rx_handler == NULL) {
532 if (ulen < sizeof(struct debugnet_ack))
533 DNETDEBUG("ignoring small ACK packet\n");
535 DNETDEBUG("ignoring unexpected non-ACK packet on "
536 "half-duplex connection.\n");
540 debugnet_handle_rx_msg(pcb, mb);
544 * Handler for incoming packets directly from the network adapter
545 * Identifies the packet type (IP or ARP) and passes it along to one of the
546 * helper functions debugnet_handle_ip or debugnet_handle_arp.
548 * It needs to partially replicate the behaviour of ether_input() and
552 * ifp the interface the packet came from
553 * m an mbuf containing the packet received
556 debugnet_input_one(struct ifnet *ifp, struct mbuf *m)
559 struct ether_header *eh;
562 /* Ethernet processing. */
563 if ((m->m_flags & M_PKTHDR) == 0) {
564 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
567 if (m->m_len < ETHER_HDR_LEN) {
569 "discard frame without leading eth header (len %u pktlen %u)\n",
570 m->m_len, m->m_pkthdr.len);
573 if ((m->m_flags & M_HASFCS) != 0) {
574 m_adj(m, -ETHER_CRC_LEN);
575 m->m_flags &= ~M_HASFCS;
577 eh = mtod(m, struct ether_header *);
578 etype = ntohs(eh->ether_type);
579 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
580 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
583 if (if_gethwaddr(ifp, &ifr) != 0) {
584 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
587 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
588 ETHER_ADDR_LEN) != 0 &&
589 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
591 "discard frame with incorrect destination addr\n");
595 MPASS(g_debugnet_pcb_inuse);
597 /* Done ethernet processing. Strip off the ethernet header. */
598 m_adj(m, ETHER_HDR_LEN);
601 debugnet_handle_arp(&g_dnet_pcb, &m);
604 debugnet_handle_ip(&g_dnet_pcb, &m);
607 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
616 debugnet_input(struct ifnet *ifp, struct mbuf *m)
623 debugnet_input_one(ifp, m);
629 * Network polling primitive.
631 * Instead of assuming that most of the network stack is sane, we just poll the
632 * driver directly for packets.
635 debugnet_network_poll(struct debugnet_pcb *pcb)
640 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
644 * Start of consumer API surface.
647 debugnet_free(struct debugnet_pcb *pcb)
651 MPASS(pcb == &g_dnet_pcb);
652 MPASS(pcb->dp_drv_input == NULL || g_debugnet_pcb_inuse);
656 if (pcb->dp_drv_input != NULL)
657 ifp->if_input = pcb->dp_drv_input;
658 if (pcb->dp_event_started)
659 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
661 debugnet_mbuf_finish();
663 g_debugnet_pcb_inuse = false;
664 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
668 debugnet_connect(const struct debugnet_conn_params *dcp,
669 struct debugnet_pcb **pcb_out)
671 struct debugnet_proto_aux herald_auxdata;
672 struct debugnet_pcb *pcb;
676 if (g_debugnet_pcb_inuse) {
677 printf("%s: Only one connection at a time.\n", __func__);
682 *pcb = (struct debugnet_pcb) {
683 .dp_state = DN_STATE_INIT,
684 .dp_client = dcp->dc_client,
685 .dp_server = dcp->dc_server,
686 .dp_gateway = dcp->dc_gateway,
687 .dp_server_port = dcp->dc_herald_port, /* Initially */
688 .dp_client_port = dcp->dc_client_port,
690 .dp_ifp = dcp->dc_ifp,
691 .dp_rx_handler = dcp->dc_rx_handler,
692 .dp_drv_input = NULL,
695 /* Switch to the debugnet mbuf zones. */
696 debugnet_mbuf_start();
698 /* At least one needed parameter is missing; infer it. */
699 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
700 pcb->dp_ifp == NULL) {
701 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
702 struct ifnet *rt_ifp;
703 struct nhop_object *nh;
705 memset(&dest_sin, 0, sizeof(dest_sin));
706 dest_sin = (struct sockaddr_in) {
707 .sin_len = sizeof(dest_sin),
708 .sin_family = AF_INET,
709 .sin_addr.s_addr = pcb->dp_server,
713 nh = fib4_lookup_debugnet(debugnet_fib, dest_sin.sin_addr, 0,
718 printf("%s: Could not get route for that server.\n",
724 /* TODO support AF_INET6 */
725 if (nh->gw_sa.sa_family == AF_INET)
726 gw_sin = &nh->gw4_sa;
728 if (nh->gw_sa.sa_family == AF_LINK)
729 DNETDEBUG("Destination address is on link.\n");
733 MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
734 local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
738 if (pcb->dp_client == INADDR_ANY)
739 pcb->dp_client = local_sin->sin_addr.s_addr;
740 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
741 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
742 if (pcb->dp_ifp == NULL)
743 pcb->dp_ifp = rt_ifp;
748 if (debugnet_debug > 0) {
749 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
750 gwbuf[INET_ADDRSTRLEN];
751 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
752 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
753 if (pcb->dp_gateway != INADDR_ANY)
754 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
755 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
756 serbuf, pcb->dp_server_port,
757 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
758 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
759 clibuf, pcb->dp_client_port, if_name(ifp));
762 /* Validate iface is online and supported. */
763 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
764 printf("%s: interface '%s' does not support debugnet\n",
765 __func__, if_name(ifp));
769 if ((if_getflags(ifp) & IFF_UP) == 0) {
770 printf("%s: interface '%s' link is down\n", __func__,
776 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
777 pcb->dp_event_started = true;
780 * We maintain the invariant that g_debugnet_pcb_inuse is always true
781 * while the debugnet ifp's if_input is overridden with
784 g_debugnet_pcb_inuse = true;
786 /* Make the card use *our* receive callback. */
787 pcb->dp_drv_input = ifp->if_input;
788 ifp->if_input = debugnet_input;
790 printf("%s: searching for %s MAC...\n", __func__,
791 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
793 error = debugnet_arp_gw(pcb);
795 printf("%s: failed to locate MAC address\n", __func__);
798 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
800 herald_auxdata = (struct debugnet_proto_aux) {
801 .dp_offset_start = dcp->dc_herald_offset,
802 .dp_aux2 = dcp->dc_herald_aux2,
804 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
805 dcp->dc_herald_datalen, &herald_auxdata);
807 printf("%s: failed to herald debugnet server\n", __func__);
820 * Pre-allocated dump-time mbuf tracking.
822 * We just track the high water mark we've ever seen and allocate appropriately
823 * for that iface/mtu combo.
830 static struct mtx dn_hwm_lk;
831 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
834 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
839 mtx_lock(&dn_hwm_lk);
841 if (nmbuf > dn_hwm.nmbuf) {
843 dn_hwm.nmbuf = nmbuf;
845 nmbuf = dn_hwm.nmbuf;
847 if (ncl > dn_hwm.ncl) {
853 if (clsize > dn_hwm.clsize) {
855 dn_hwm.clsize = clsize;
857 clsize = dn_hwm.clsize;
859 mtx_unlock(&dn_hwm_lk);
862 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
866 debugnet_any_ifnet_update(struct ifnet *ifp)
868 int clsize, nmbuf, ncl, nrxr;
870 if (!DEBUGNET_SUPPORTED_NIC(ifp))
873 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
874 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
877 * We need two headers per message on the transmit side. Multiply by
878 * four to give us some breathing room.
880 nmbuf = ncl * (4 + nrxr);
884 * Bandaid for drivers that (incorrectly) advertise LinkUp before their
885 * dn_init method is available.
887 if (nmbuf == 0 || ncl == 0 || clsize == 0) {
891 printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
892 __func__, if_name(ifp), ifp);
895 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
899 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
900 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
902 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
903 * because the driver is still in attach. Since we cannot use down interfaces,
904 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
905 * with vtnet and dhcpclient that event just never occurs.
907 * So that's how I've landed on the lower level ifnet_link_event.
911 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
913 if (link_state == LINK_STATE_UP)
914 debugnet_any_ifnet_update(ifp);
917 static eventhandler_tag dn_attach_cookie;
919 dn_evh_init(void *ctx __unused)
921 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
922 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
924 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
927 * DDB parsing helpers for debugnet(4) consumers.
932 const char *printname;
937 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
944 for (octet = 0; octet < 4; octet++) {
945 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
947 db_printf("%s:%s: octet %u expected number; found %d\n",
948 __func__, opt->printname, octet, t);
952 * db_lex lexes '-' distinctly from the number itself, but
953 * let's document that invariant.
955 MPASS(db_tok_number >= 0);
957 if (db_tok_number > UINT8_MAX) {
958 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
959 opt->printname, octet, (intmax_t)db_tok_number);
963 /* Constructed host-endian and converted to network later. */
964 tmp = (tmp << 8) | db_tok_number;
967 t = db_read_token_flags(DRT_WSPACE);
969 db_printf("%s:%s: octet %u expected '.'; found"
970 " %d\n", __func__, opt->printname, octet,
977 *opt->result = htonl(tmp);
983 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
990 struct my_inet_opt opt_client = {
991 .printname = "client",
992 .result = &result->dd_client,
995 .printname = "server",
996 .result = &result->dd_server,
999 .printname = "gateway",
1000 .result = &result->dd_gateway,
1005 memset(result, 0, sizeof(*result));
1008 * command [space] [-] [opt] [[space] [optarg]] ...
1010 * db_command has already lexed 'command' for us.
1012 t = db_read_token_flags(DRT_WSPACE);
1014 t = db_read_token_flags(DRT_WSPACE);
1018 db_printf("%s: Bad syntax; expected '-', got %d\n",
1023 t = db_read_token_flags(DRT_WSPACE);
1025 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
1030 if (strlen(db_tok_string) > 1) {
1031 db_printf("%s: Bad syntax; expected single option "
1032 "flag, got '%s'\n", cmd, db_tok_string);
1037 cur_inet_opt = NULL;
1038 switch ((ch = db_tok_string[0])) {
1040 DNETDEBUG("Unexpected: '%c'\n", ch);
1045 cur_inet_opt = &opt_client;
1048 cur_inet_opt = &opt_gateway;
1051 cur_inet_opt = &opt_server;
1058 t = db_read_token_flags(DRT_WSPACE);
1060 db_printf("%s: Bad syntax; expected space after "
1061 "flag %c, got %d\n", cmd, ch, t);
1066 t = db_read_token_flags(DRT_WSPACE);
1068 db_printf("%s: Expected interface but got %d\n",
1075 * We *don't* take a ref here because the only current
1076 * consumer, db_netdump_cmd, does not need it. It
1077 * (somewhat redundantly) extracts the if_name(),
1078 * re-lookups the ifp, and takes its own reference.
1080 ifp = ifunit(db_tok_string);
1083 db_printf("Could not locate interface %s\n",
1089 MPASS(cur_inet_opt != NULL);
1090 /* Assume IPv4 for now. */
1091 error = dn_parse_optarg_ipv4(cur_inet_opt);
1096 /* Skip (mandatory) whitespace after option, if not EOL. */
1097 t = db_read_token_flags(DRT_WSPACE);
1101 db_printf("%s: Bad syntax; expected space after "
1102 "flag %c option; got %d\n", cmd, ch, t);
1105 t = db_read_token_flags(DRT_WSPACE);
1108 if (!opt_server.has_opt) {
1109 db_printf("%s: need a destination server address\n", cmd);
1113 result->dd_has_client = opt_client.has_opt;
1114 result->dd_has_gateway = opt_gateway.has_opt;
1115 result->dd_ifp = ifp;
1117 /* We parsed the full line to tEOL already, or bailed with an error. */
1121 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1122 "-i <interface>]\n", cmd);