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>
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/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
50 #include <ddb/db_lex.h>
53 #include <net/ethernet.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_var.h>
59 #include <net/if_private.h>
61 #include <net/route.h>
62 #include <net/route/nhop.h>
64 #include <netinet/in.h>
65 #include <netinet/in_fib.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_options.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
74 #include <machine/in_cksum.h>
75 #include <machine/pcb.h>
77 #include <net/debugnet.h>
78 #define DEBUGNET_INTERNAL
79 #include <net/debugnet_int.h>
81 FEATURE(debugnet, "Debugnet support");
83 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
84 "debugnet parameters");
86 unsigned debugnet_debug;
87 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
89 "Debug message verbosity (0: off; 1: on; 2: verbose)");
91 int debugnet_npolls = 2000;
92 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
94 "Number of times to poll before assuming packet loss (0.5ms per poll)");
95 int debugnet_nretries = 10;
96 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
97 &debugnet_nretries, 0,
98 "Number of retransmit attempts before giving up");
99 int debugnet_fib = RT_DEFAULT_FIB;
100 SYSCTL_INT(_net_debugnet, OID_AUTO, fib, CTLFLAG_RWTUN,
102 "Fib to use when sending dump");
104 static bool g_debugnet_pcb_inuse;
105 static struct debugnet_pcb g_dnet_pcb;
108 * Simple accessors for opaque PCB.
110 const unsigned char *
111 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
113 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
114 pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
115 return (pcb->dp_gw_mac.octet);
119 debugnet_get_server_addr(const struct debugnet_pcb *pcb)
121 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
122 pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
123 return (&pcb->dp_server);
127 debugnet_get_server_port(const struct debugnet_pcb *pcb)
129 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
130 pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
131 return (pcb->dp_server_port);
135 * Start of network primitives, beginning with output primitives.
139 * Handles creation of the ethernet header, then places outgoing packets into
140 * the tx buffer for the NIC
143 * m The mbuf containing the packet to be sent (will be freed by
144 * this function or the NIC driver)
145 * ifp The interface to send on
146 * dst The destination ethernet address (source address will be looked
148 * etype The ETHERTYPE_* value for the protocol that is being sent
151 * int see errno.h, 0 for success
154 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
157 struct ether_header *eh;
159 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
160 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
161 if_printf(ifp, "%s: interface isn't up\n", __func__);
166 /* Fill in the ethernet header. */
167 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
169 printf("%s: out of mbufs\n", __func__);
172 eh = mtod(m, struct ether_header *);
173 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
174 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
175 eh->ether_type = htons(etype);
176 return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
180 * Unreliable transmission of an mbuf chain to the debugnet server
181 * Note: can't handle fragmentation; fails if the packet is larger than
182 * ifp->if_mtu after adding the UDP/IP headers
185 * pcb The debugnet context block
189 * int see errno.h, 0 for success
192 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
196 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
198 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
200 printf("%s: out of mbufs\n", __func__);
204 udp = mtod(m, void *);
205 udp->uh_ulen = htons(m->m_pkthdr.len);
206 /* Use this src port so that the server can connect() the socket */
207 udp->uh_sport = htons(pcb->dp_client_port);
208 udp->uh_dport = htons(pcb->dp_server_port);
209 /* Computed later (protocol-dependent). */
212 return (debugnet_ip_output(pcb, m));
216 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
218 struct debugnet_ack *dn_ack;
221 DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
223 m = m_gethdr(M_NOWAIT, MT_DATA);
225 printf("%s: Out of mbufs\n", __func__);
228 m->m_len = sizeof(*dn_ack);
229 m->m_pkthdr.len = sizeof(*dn_ack);
230 MH_ALIGN(m, sizeof(*dn_ack));
231 dn_ack = mtod(m, void *);
232 dn_ack->da_seqno = seqno;
234 return (debugnet_udp_output(pcb, m));
238 * Dummy free function for debugnet clusters.
241 debugnet_mbuf_free(struct mbuf *m __unused)
246 * Construct and reliably send a debugnet packet. May fail from a resource
247 * shortage or extreme number of unacknowledged retransmissions. Wait for
248 * an acknowledgement before returning. Splits packets into chunks small
249 * enough to be sent without fragmentation (looks up the interface MTU)
252 * type debugnet packet type (HERALD, FINISHED, ...)
254 * datalen data size (bytes)
255 * auxdata optional auxiliary information
258 * int see errno.h, 0 for success
261 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
262 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
264 struct debugnet_msg_hdr *dn_msg_hdr;
267 uint32_t i, pktlen, sent_so_far;
268 int retries, polls, error;
270 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
274 pcb->dp_rcvd_acks = 0;
278 /* Chunks can be too big to fit in packets. */
279 for (i = sent_so_far = 0; sent_so_far < datalen ||
280 (i == 0 && datalen == 0); i++) {
281 pktlen = datalen - sent_so_far;
283 /* Bound: the interface MTU (assume no IP options). */
284 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
285 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
288 * Check if it is retransmitting and this has been ACKed
291 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
292 sent_so_far += pktlen;
297 * Get and fill a header mbuf, then chain data as an extended
300 m = m_gethdr(M_NOWAIT, MT_DATA);
302 printf("%s: Out of mbufs\n", __func__);
305 m->m_len = sizeof(struct debugnet_msg_hdr);
306 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
307 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
308 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
309 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
310 dn_msg_hdr->mh_type = htonl(type);
311 dn_msg_hdr->mh_len = htonl(pktlen);
313 if (auxdata != NULL) {
314 dn_msg_hdr->mh_offset =
315 htobe64(auxdata->dp_offset_start + sent_so_far);
316 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
318 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
319 dn_msg_hdr->mh_aux2 = 0;
323 m2 = m_get(M_NOWAIT, MT_DATA);
326 printf("%s: Out of mbufs\n", __func__);
329 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
330 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
335 m->m_pkthdr.len += pktlen;
337 error = debugnet_udp_output(pcb, m);
341 /* Note that we're waiting for this packet in the bitfield. */
342 want_acks |= (1 << i);
343 sent_so_far += pktlen;
345 if (i >= DEBUGNET_MAX_IN_FLIGHT)
346 printf("Warning: Sent more than %d packets (%d). "
347 "Acknowledgements will fail unless the size of "
348 "rcvd_acks/want_acks is increased.\n",
349 DEBUGNET_MAX_IN_FLIGHT, i);
352 * Wait for acks. A *real* window would speed things up considerably.
355 while (pcb->dp_rcvd_acks != want_acks) {
356 if (polls++ > debugnet_npolls) {
357 if (retries++ > debugnet_nretries)
362 debugnet_network_poll(pcb);
364 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
372 * Network input primitives.
376 * Just introspect the header enough to fire off a seqno ack and validate
380 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
382 const struct debugnet_msg_hdr *dnh;
390 if (m->m_pkthdr.len < sizeof(*dnh)) {
391 DNETDEBUG("ignoring small debugnet_msg packet\n");
396 if (m->m_len < sizeof(*dnh)) {
397 m = m_pullup(m, sizeof(*dnh));
400 DNETDEBUG("m_pullup failed\n");
405 dnh = mtod(m, const void *);
406 if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
407 DNETDEBUG("Dropping short packet.\n");
411 hdr_type = ntohl(dnh->mh_type);
412 if (hdr_type != DEBUGNET_DATA) {
413 if (hdr_type == DEBUGNET_FINISHED) {
414 printf("Remote shut down the connection on us!\n");
415 pcb->dp_state = DN_STATE_REMOTE_CLOSED;
416 if (pcb->dp_finish_handler != NULL) {
417 pcb->dp_finish_handler();
420 DNETDEBUG("Got unexpected debugnet message %u\n", hdr_type);
426 * If the issue is transient (ENOBUFS), sender should resend. If
427 * non-transient (like driver objecting to rx -> tx from the same
428 * thread), not much else we can do.
430 seqno = dnh->mh_seqno; /* net endian */
431 m_adj(m, sizeof(*dnh));
433 error = pcb->dp_rx_handler(m);
435 DNETDEBUG("RX handler was not able to accept message, error %d. "
436 "Skipping ack.\n", error);
440 error = debugnet_ack_output(pcb, seqno);
442 DNETDEBUG("Couldn't ACK rx packet %u; %d\n", ntohl(seqno), error);
447 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
449 const struct debugnet_ack *dn_ack;
456 if (m->m_len < sizeof(*dn_ack)) {
457 m = m_pullup(m, sizeof(*dn_ack));
460 DNETDEBUG("m_pullup failed\n");
464 dn_ack = mtod(m, const void *);
466 /* Debugnet processing. */
468 * Packet is meant for us. Extract the ack sequence number and the
469 * port number if necessary.
471 rcv_ackno = ntohl(dn_ack->da_seqno);
472 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
473 pcb->dp_server_port = sport;
474 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
476 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
477 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
478 else if (rcv_ackno >= pcb->dp_seqno) {
479 /* We're interested in this ack. Record it. */
480 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
485 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
487 const struct udphdr *udp;
489 uint16_t sport, ulen;
491 /* UDP processing. */
494 if (m->m_pkthdr.len < sizeof(*udp)) {
495 DNETDEBUG("ignoring small UDP packet\n");
499 /* Get UDP headers. */
500 if (m->m_len < sizeof(*udp)) {
501 m = m_pullup(m, sizeof(*udp));
504 DNETDEBUG("m_pullup failed\n");
508 udp = mtod(m, const void *);
510 /* We expect to receive UDP packets on the configured client port. */
511 if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
512 DNETDEBUG("not on the expected port.\n");
516 /* Check that ulen does not exceed actual size of data. */
517 ulen = ntohs(udp->uh_ulen);
518 if (m->m_pkthdr.len < ulen) {
519 DNETDEBUG("ignoring runt UDP packet\n");
523 sport = ntohs(udp->uh_sport);
525 m_adj(m, sizeof(*udp));
526 ulen -= sizeof(*udp);
528 if (ulen == sizeof(struct debugnet_ack)) {
529 debugnet_handle_ack(pcb, mb, sport);
533 if (pcb->dp_rx_handler == NULL) {
534 if (ulen < sizeof(struct debugnet_ack))
535 DNETDEBUG("ignoring small ACK packet\n");
537 DNETDEBUG("ignoring unexpected non-ACK packet on "
538 "half-duplex connection.\n");
542 debugnet_handle_rx_msg(pcb, mb);
546 * Handler for incoming packets directly from the network adapter
547 * Identifies the packet type (IP or ARP) and passes it along to one of the
548 * helper functions debugnet_handle_ip or debugnet_handle_arp.
550 * It needs to partially replicate the behaviour of ether_input() and
554 * ifp the interface the packet came from
555 * m an mbuf containing the packet received
558 debugnet_input_one(struct ifnet *ifp, struct mbuf *m)
561 struct ether_header *eh;
564 /* Ethernet processing. */
565 if ((m->m_flags & M_PKTHDR) == 0) {
566 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
569 if (m->m_len < ETHER_HDR_LEN) {
571 "discard frame without leading eth header (len %u pktlen %u)\n",
572 m->m_len, m->m_pkthdr.len);
575 if ((m->m_flags & M_HASFCS) != 0) {
576 m_adj(m, -ETHER_CRC_LEN);
577 m->m_flags &= ~M_HASFCS;
579 eh = mtod(m, struct ether_header *);
580 etype = ntohs(eh->ether_type);
581 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
582 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
585 if (if_gethwaddr(ifp, &ifr) != 0) {
586 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
589 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
590 ETHER_ADDR_LEN) != 0 &&
591 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
593 "discard frame with incorrect destination addr\n");
597 MPASS(g_debugnet_pcb_inuse);
599 /* Done ethernet processing. Strip off the ethernet header. */
600 m_adj(m, ETHER_HDR_LEN);
603 debugnet_handle_arp(&g_dnet_pcb, &m);
606 debugnet_handle_ip(&g_dnet_pcb, &m);
609 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
618 debugnet_input(struct ifnet *ifp, struct mbuf *m)
625 debugnet_input_one(ifp, m);
631 * Network polling primitive.
633 * Instead of assuming that most of the network stack is sane, we just poll the
634 * driver directly for packets.
637 debugnet_network_poll(struct debugnet_pcb *pcb)
642 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
646 * Start of consumer API surface.
649 debugnet_free(struct debugnet_pcb *pcb)
653 MPASS(pcb == &g_dnet_pcb);
654 MPASS(pcb->dp_drv_input == NULL || g_debugnet_pcb_inuse);
658 if (pcb->dp_drv_input != NULL)
659 ifp->if_input = pcb->dp_drv_input;
660 if (pcb->dp_event_started)
661 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
663 debugnet_mbuf_finish();
665 g_debugnet_pcb_inuse = false;
666 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
670 debugnet_connect(const struct debugnet_conn_params *dcp,
671 struct debugnet_pcb **pcb_out)
673 struct debugnet_proto_aux herald_auxdata;
674 struct debugnet_pcb *pcb;
678 if (g_debugnet_pcb_inuse) {
679 printf("%s: Only one connection at a time.\n", __func__);
684 *pcb = (struct debugnet_pcb) {
685 .dp_state = DN_STATE_INIT,
686 .dp_client = dcp->dc_client,
687 .dp_server = dcp->dc_server,
688 .dp_gateway = dcp->dc_gateway,
689 .dp_server_port = dcp->dc_herald_port, /* Initially */
690 .dp_client_port = dcp->dc_client_port,
692 .dp_ifp = dcp->dc_ifp,
693 .dp_rx_handler = dcp->dc_rx_handler,
694 .dp_drv_input = NULL,
697 /* Switch to the debugnet mbuf zones. */
698 debugnet_mbuf_start();
700 /* At least one needed parameter is missing; infer it. */
701 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
702 pcb->dp_ifp == NULL) {
703 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
704 struct ifnet *rt_ifp;
705 struct nhop_object *nh;
707 memset(&dest_sin, 0, sizeof(dest_sin));
708 dest_sin = (struct sockaddr_in) {
709 .sin_len = sizeof(dest_sin),
710 .sin_family = AF_INET,
711 .sin_addr.s_addr = pcb->dp_server,
715 nh = fib4_lookup_debugnet(debugnet_fib, dest_sin.sin_addr, 0,
720 printf("%s: Could not get route for that server.\n",
726 /* TODO support AF_INET6 */
727 if (nh->gw_sa.sa_family == AF_INET)
728 gw_sin = &nh->gw4_sa;
730 if (nh->gw_sa.sa_family == AF_LINK)
731 DNETDEBUG("Destination address is on link.\n");
735 MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
736 local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
740 if (pcb->dp_client == INADDR_ANY)
741 pcb->dp_client = local_sin->sin_addr.s_addr;
742 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
743 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
744 if (pcb->dp_ifp == NULL)
745 pcb->dp_ifp = rt_ifp;
750 if (debugnet_debug > 0) {
751 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
752 gwbuf[INET_ADDRSTRLEN];
753 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
754 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
755 if (pcb->dp_gateway != INADDR_ANY)
756 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
757 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
758 serbuf, pcb->dp_server_port,
759 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
760 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
761 clibuf, pcb->dp_client_port, if_name(ifp));
764 /* Validate iface is online and supported. */
765 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
766 printf("%s: interface '%s' does not support debugnet\n",
767 __func__, if_name(ifp));
771 if ((if_getflags(ifp) & IFF_UP) == 0) {
772 printf("%s: interface '%s' link is down\n", __func__,
778 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
779 pcb->dp_event_started = true;
782 * We maintain the invariant that g_debugnet_pcb_inuse is always true
783 * while the debugnet ifp's if_input is overridden with
786 g_debugnet_pcb_inuse = true;
788 /* Make the card use *our* receive callback. */
789 pcb->dp_drv_input = ifp->if_input;
790 ifp->if_input = debugnet_input;
792 printf("%s: searching for %s MAC...\n", __func__,
793 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
795 error = debugnet_arp_gw(pcb);
797 printf("%s: failed to locate MAC address\n", __func__);
800 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
802 herald_auxdata = (struct debugnet_proto_aux) {
803 .dp_offset_start = dcp->dc_herald_offset,
804 .dp_aux2 = dcp->dc_herald_aux2,
806 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
807 dcp->dc_herald_datalen, &herald_auxdata);
809 printf("%s: failed to herald debugnet server\n", __func__);
822 * Pre-allocated dump-time mbuf tracking.
824 * We just track the high water mark we've ever seen and allocate appropriately
825 * for that iface/mtu combo.
832 static struct mtx dn_hwm_lk;
833 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
836 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
841 mtx_lock(&dn_hwm_lk);
843 if (nmbuf > dn_hwm.nmbuf) {
845 dn_hwm.nmbuf = nmbuf;
847 nmbuf = dn_hwm.nmbuf;
849 if (ncl > dn_hwm.ncl) {
855 if (clsize > dn_hwm.clsize) {
857 dn_hwm.clsize = clsize;
859 clsize = dn_hwm.clsize;
861 mtx_unlock(&dn_hwm_lk);
864 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
868 debugnet_any_ifnet_update(struct ifnet *ifp)
870 int clsize, nmbuf, ncl, nrxr;
872 if (!DEBUGNET_SUPPORTED_NIC(ifp))
875 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
876 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
879 * We need two headers per message on the transmit side. Multiply by
880 * four to give us some breathing room.
882 nmbuf = ncl * (4 + nrxr);
886 * Bandaid for drivers that (incorrectly) advertise LinkUp before their
887 * dn_init method is available.
889 if (nmbuf == 0 || ncl == 0 || clsize == 0) {
893 printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
894 __func__, if_name(ifp), ifp);
897 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
901 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
902 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
904 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
905 * because the driver is still in attach. Since we cannot use down interfaces,
906 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
907 * with vtnet and dhcpclient that event just never occurs.
909 * So that's how I've landed on the lower level ifnet_link_event.
913 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
915 if (link_state == LINK_STATE_UP)
916 debugnet_any_ifnet_update(ifp);
919 static eventhandler_tag dn_attach_cookie;
921 dn_evh_init(void *ctx __unused)
923 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
924 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
926 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
929 * DDB parsing helpers for debugnet(4) consumers.
934 const char *printname;
939 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
946 for (octet = 0; octet < 4; octet++) {
947 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
949 db_printf("%s:%s: octet %u expected number; found %d\n",
950 __func__, opt->printname, octet, t);
954 * db_lex lexes '-' distinctly from the number itself, but
955 * let's document that invariant.
957 MPASS(db_tok_number >= 0);
959 if (db_tok_number > UINT8_MAX) {
960 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
961 opt->printname, octet, (intmax_t)db_tok_number);
965 /* Constructed host-endian and converted to network later. */
966 tmp = (tmp << 8) | db_tok_number;
969 t = db_read_token_flags(DRT_WSPACE);
971 db_printf("%s:%s: octet %u expected '.'; found"
972 " %d\n", __func__, opt->printname, octet,
979 *opt->result = htonl(tmp);
985 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
992 struct my_inet_opt opt_client = {
993 .printname = "client",
994 .result = &result->dd_client,
997 .printname = "server",
998 .result = &result->dd_server,
1001 .printname = "gateway",
1002 .result = &result->dd_gateway,
1007 memset(result, 0, sizeof(*result));
1010 * command [space] [-] [opt] [[space] [optarg]] ...
1012 * db_command has already lexed 'command' for us.
1014 t = db_read_token_flags(DRT_WSPACE);
1016 t = db_read_token_flags(DRT_WSPACE);
1020 db_printf("%s: Bad syntax; expected '-', got %d\n",
1025 t = db_read_token_flags(DRT_WSPACE);
1027 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
1032 if (strlen(db_tok_string) > 1) {
1033 db_printf("%s: Bad syntax; expected single option "
1034 "flag, got '%s'\n", cmd, db_tok_string);
1039 cur_inet_opt = NULL;
1040 switch ((ch = db_tok_string[0])) {
1042 DNETDEBUG("Unexpected: '%c'\n", ch);
1047 cur_inet_opt = &opt_client;
1050 cur_inet_opt = &opt_gateway;
1053 cur_inet_opt = &opt_server;
1060 t = db_read_token_flags(DRT_WSPACE);
1062 db_printf("%s: Bad syntax; expected space after "
1063 "flag %c, got %d\n", cmd, ch, t);
1068 t = db_read_token_flags(DRT_WSPACE);
1070 db_printf("%s: Expected interface but got %d\n",
1077 * We *don't* take a ref here because the only current
1078 * consumer, db_netdump_cmd, does not need it. It
1079 * (somewhat redundantly) extracts the if_name(),
1080 * re-lookups the ifp, and takes its own reference.
1082 ifp = ifunit(db_tok_string);
1085 db_printf("Could not locate interface %s\n",
1091 MPASS(cur_inet_opt != NULL);
1092 /* Assume IPv4 for now. */
1093 error = dn_parse_optarg_ipv4(cur_inet_opt);
1098 /* Skip (mandatory) whitespace after option, if not EOL. */
1099 t = db_read_token_flags(DRT_WSPACE);
1103 db_printf("%s: Bad syntax; expected space after "
1104 "flag %c option; got %d\n", cmd, ch, t);
1107 t = db_read_token_flags(DRT_WSPACE);
1110 if (!opt_server.has_opt) {
1111 db_printf("%s: need a destination server address\n", cmd);
1115 result->dd_has_client = opt_client.has_opt;
1116 result->dd_has_gateway = opt_gateway.has_opt;
1117 result->dd_ifp = ifp;
1119 /* We parsed the full line to tEOL already, or bailed with an error. */
1123 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1124 "-i <interface>]\n", cmd);