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/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 * Start of network primitives, beginning with output primitives.
123 * Handles creation of the ethernet header, then places outgoing packets into
124 * the tx buffer for the NIC
127 * m The mbuf containing the packet to be sent (will be freed by
128 * this function or the NIC driver)
129 * ifp The interface to send on
130 * dst The destination ethernet address (source address will be looked
132 * etype The ETHERTYPE_* value for the protocol that is being sent
135 * int see errno.h, 0 for success
138 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
141 struct ether_header *eh;
143 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
144 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
145 if_printf(ifp, "%s: interface isn't up\n", __func__);
150 /* Fill in the ethernet header. */
151 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
153 printf("%s: out of mbufs\n", __func__);
156 eh = mtod(m, struct ether_header *);
157 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
158 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
159 eh->ether_type = htons(etype);
160 return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
164 * Unreliable transmission of an mbuf chain to the debugnet server
165 * Note: can't handle fragmentation; fails if the packet is larger than
166 * ifp->if_mtu after adding the UDP/IP headers
169 * pcb The debugnet context block
173 * int see errno.h, 0 for success
176 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
180 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
182 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
184 printf("%s: out of mbufs\n", __func__);
188 udp = mtod(m, void *);
189 udp->uh_ulen = htons(m->m_pkthdr.len);
190 /* Use this src port so that the server can connect() the socket */
191 udp->uh_sport = htons(pcb->dp_client_port);
192 udp->uh_dport = htons(pcb->dp_server_port);
193 /* Computed later (protocol-dependent). */
196 return (debugnet_ip_output(pcb, m));
200 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
202 struct debugnet_ack *dn_ack;
205 DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
207 m = m_gethdr(M_NOWAIT, MT_DATA);
209 printf("%s: Out of mbufs\n", __func__);
212 m->m_len = sizeof(*dn_ack);
213 m->m_pkthdr.len = sizeof(*dn_ack);
214 MH_ALIGN(m, sizeof(*dn_ack));
215 dn_ack = mtod(m, void *);
216 dn_ack->da_seqno = seqno;
218 return (debugnet_udp_output(pcb, m));
222 * Dummy free function for debugnet clusters.
225 debugnet_mbuf_free(struct mbuf *m __unused)
230 * Construct and reliably send a debugnet packet. May fail from a resource
231 * shortage or extreme number of unacknowledged retransmissions. Wait for
232 * an acknowledgement before returning. Splits packets into chunks small
233 * enough to be sent without fragmentation (looks up the interface MTU)
236 * type debugnet packet type (HERALD, FINISHED, ...)
238 * datalen data size (bytes)
239 * auxdata optional auxiliary information
242 * int see errno.h, 0 for success
245 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
246 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
248 struct debugnet_msg_hdr *dn_msg_hdr;
251 uint32_t i, pktlen, sent_so_far;
252 int retries, polls, error;
254 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
258 pcb->dp_rcvd_acks = 0;
262 /* Chunks can be too big to fit in packets. */
263 for (i = sent_so_far = 0; sent_so_far < datalen ||
264 (i == 0 && datalen == 0); i++) {
265 pktlen = datalen - sent_so_far;
267 /* Bound: the interface MTU (assume no IP options). */
268 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
269 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
272 * Check if it is retransmitting and this has been ACKed
275 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
276 sent_so_far += pktlen;
281 * Get and fill a header mbuf, then chain data as an extended
284 m = m_gethdr(M_NOWAIT, MT_DATA);
286 printf("%s: Out of mbufs\n", __func__);
289 m->m_len = sizeof(struct debugnet_msg_hdr);
290 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
291 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
292 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
293 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
294 dn_msg_hdr->mh_type = htonl(type);
295 dn_msg_hdr->mh_len = htonl(pktlen);
297 if (auxdata != NULL) {
298 dn_msg_hdr->mh_offset =
299 htobe64(auxdata->dp_offset_start + sent_so_far);
300 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
302 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
303 dn_msg_hdr->mh_aux2 = 0;
307 m2 = m_get(M_NOWAIT, MT_DATA);
310 printf("%s: Out of mbufs\n", __func__);
313 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
314 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
319 m->m_pkthdr.len += pktlen;
321 error = debugnet_udp_output(pcb, m);
325 /* Note that we're waiting for this packet in the bitfield. */
326 want_acks |= (1 << i);
327 sent_so_far += pktlen;
329 if (i >= DEBUGNET_MAX_IN_FLIGHT)
330 printf("Warning: Sent more than %d packets (%d). "
331 "Acknowledgements will fail unless the size of "
332 "rcvd_acks/want_acks is increased.\n",
333 DEBUGNET_MAX_IN_FLIGHT, i);
336 * Wait for acks. A *real* window would speed things up considerably.
339 while (pcb->dp_rcvd_acks != want_acks) {
340 if (polls++ > debugnet_npolls) {
341 if (retries++ > debugnet_nretries)
346 debugnet_network_poll(pcb);
348 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
356 * Network input primitives.
360 * Just introspect the header enough to fire off a seqno ack and validate
364 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
366 const struct debugnet_msg_hdr *dnh;
372 if (m->m_pkthdr.len < sizeof(*dnh)) {
373 DNETDEBUG("ignoring small debugnet_msg packet\n");
378 if (m->m_len < sizeof(*dnh)) {
379 m = m_pullup(m, sizeof(*dnh));
382 DNETDEBUG("m_pullup failed\n");
386 dnh = mtod(m, const void *);
388 if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
389 DNETDEBUG("Dropping short packet.\n");
394 * If the issue is transient (ENOBUFS), sender should resend. If
395 * non-transient (like driver objecting to rx -> tx from the same
396 * thread), not much else we can do.
398 error = debugnet_ack_output(pcb, dnh->mh_seqno);
402 if (ntohl(dnh->mh_type) == DEBUGNET_FINISHED) {
403 printf("Remote shut down the connection on us!\n");
404 pcb->dp_state = DN_STATE_REMOTE_CLOSED;
407 * Continue through to the user handler so they are signalled
408 * not to wait for further rx.
412 pcb->dp_rx_handler(pcb, mb);
416 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
418 const struct debugnet_ack *dn_ack;
425 if (m->m_len < sizeof(*dn_ack)) {
426 m = m_pullup(m, sizeof(*dn_ack));
429 DNETDEBUG("m_pullup failed\n");
433 dn_ack = mtod(m, const void *);
435 /* Debugnet processing. */
437 * Packet is meant for us. Extract the ack sequence number and the
438 * port number if necessary.
440 rcv_ackno = ntohl(dn_ack->da_seqno);
441 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
442 pcb->dp_server_port = sport;
443 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
445 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
446 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
447 else if (rcv_ackno >= pcb->dp_seqno) {
448 /* We're interested in this ack. Record it. */
449 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
454 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
456 const struct udphdr *udp;
458 uint16_t sport, ulen;
460 /* UDP processing. */
463 if (m->m_pkthdr.len < sizeof(*udp)) {
464 DNETDEBUG("ignoring small UDP packet\n");
468 /* Get UDP headers. */
469 if (m->m_len < sizeof(*udp)) {
470 m = m_pullup(m, sizeof(*udp));
473 DNETDEBUG("m_pullup failed\n");
477 udp = mtod(m, const void *);
479 /* We expect to receive UDP packets on the configured client port. */
480 if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
481 DNETDEBUG("not on the expected port.\n");
485 /* Check that ulen does not exceed actual size of data. */
486 ulen = ntohs(udp->uh_ulen);
487 if (m->m_pkthdr.len < ulen) {
488 DNETDEBUG("ignoring runt UDP packet\n");
492 sport = ntohs(udp->uh_sport);
494 m_adj(m, sizeof(*udp));
495 ulen -= sizeof(*udp);
497 if (ulen == sizeof(struct debugnet_ack)) {
498 debugnet_handle_ack(pcb, mb, sport);
502 if (pcb->dp_rx_handler == NULL) {
503 if (ulen < sizeof(struct debugnet_ack))
504 DNETDEBUG("ignoring small ACK packet\n");
506 DNETDEBUG("ignoring unexpected non-ACK packet on "
507 "half-duplex connection.\n");
511 debugnet_handle_rx_msg(pcb, mb);
515 * Handler for incoming packets directly from the network adapter
516 * Identifies the packet type (IP or ARP) and passes it along to one of the
517 * helper functions debugnet_handle_ip or debugnet_handle_arp.
519 * It needs to partially replicate the behaviour of ether_input() and
523 * ifp the interface the packet came from
524 * m an mbuf containing the packet received
527 debugnet_input_one(struct ifnet *ifp, struct mbuf *m)
530 struct ether_header *eh;
533 /* Ethernet processing. */
534 if ((m->m_flags & M_PKTHDR) == 0) {
535 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
538 if (m->m_len < ETHER_HDR_LEN) {
540 "discard frame without leading eth header (len %u pktlen %u)\n",
541 m->m_len, m->m_pkthdr.len);
544 if ((m->m_flags & M_HASFCS) != 0) {
545 m_adj(m, -ETHER_CRC_LEN);
546 m->m_flags &= ~M_HASFCS;
548 eh = mtod(m, struct ether_header *);
549 etype = ntohs(eh->ether_type);
550 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
551 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
554 if (if_gethwaddr(ifp, &ifr) != 0) {
555 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
558 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
559 ETHER_ADDR_LEN) != 0 &&
560 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
562 "discard frame with incorrect destination addr\n");
566 MPASS(g_debugnet_pcb_inuse);
568 /* Done ethernet processing. Strip off the ethernet header. */
569 m_adj(m, ETHER_HDR_LEN);
572 debugnet_handle_arp(&g_dnet_pcb, &m);
575 debugnet_handle_ip(&g_dnet_pcb, &m);
578 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
587 debugnet_input(struct ifnet *ifp, struct mbuf *m)
594 debugnet_input_one(ifp, m);
600 * Network polling primitive.
602 * Instead of assuming that most of the network stack is sane, we just poll the
603 * driver directly for packets.
606 debugnet_network_poll(struct debugnet_pcb *pcb)
611 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
615 * Start of consumer API surface.
618 debugnet_free(struct debugnet_pcb *pcb)
622 MPASS(pcb == &g_dnet_pcb);
623 MPASS(pcb->dp_drv_input == NULL || g_debugnet_pcb_inuse);
627 if (pcb->dp_drv_input != NULL)
628 ifp->if_input = pcb->dp_drv_input;
629 if (pcb->dp_event_started)
630 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
632 debugnet_mbuf_finish();
634 g_debugnet_pcb_inuse = false;
635 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
639 debugnet_connect(const struct debugnet_conn_params *dcp,
640 struct debugnet_pcb **pcb_out)
642 struct debugnet_proto_aux herald_auxdata;
643 struct debugnet_pcb *pcb;
647 if (g_debugnet_pcb_inuse) {
648 printf("%s: Only one connection at a time.\n", __func__);
653 *pcb = (struct debugnet_pcb) {
654 .dp_state = DN_STATE_INIT,
655 .dp_client = dcp->dc_client,
656 .dp_server = dcp->dc_server,
657 .dp_gateway = dcp->dc_gateway,
658 .dp_server_port = dcp->dc_herald_port, /* Initially */
659 .dp_client_port = dcp->dc_client_port,
661 .dp_ifp = dcp->dc_ifp,
662 .dp_rx_handler = dcp->dc_rx_handler,
663 .dp_drv_input = NULL,
666 /* Switch to the debugnet mbuf zones. */
667 debugnet_mbuf_start();
669 /* At least one needed parameter is missing; infer it. */
670 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
671 pcb->dp_ifp == NULL) {
672 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
673 struct ifnet *rt_ifp;
674 struct nhop_object *nh;
676 memset(&dest_sin, 0, sizeof(dest_sin));
677 dest_sin = (struct sockaddr_in) {
678 .sin_len = sizeof(dest_sin),
679 .sin_family = AF_INET,
680 .sin_addr.s_addr = pcb->dp_server,
684 nh = fib4_lookup_debugnet(debugnet_fib, dest_sin.sin_addr, 0,
689 printf("%s: Could not get route for that server.\n",
695 /* TODO support AF_INET6 */
696 if (nh->gw_sa.sa_family == AF_INET)
697 gw_sin = &nh->gw4_sa;
699 if (nh->gw_sa.sa_family == AF_LINK)
700 DNETDEBUG("Destination address is on link.\n");
704 MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
705 local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
709 if (pcb->dp_client == INADDR_ANY)
710 pcb->dp_client = local_sin->sin_addr.s_addr;
711 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
712 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
713 if (pcb->dp_ifp == NULL)
714 pcb->dp_ifp = rt_ifp;
719 if (debugnet_debug > 0) {
720 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
721 gwbuf[INET_ADDRSTRLEN];
722 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
723 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
724 if (pcb->dp_gateway != INADDR_ANY)
725 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
726 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
727 serbuf, pcb->dp_server_port,
728 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
729 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
730 clibuf, pcb->dp_client_port, if_name(ifp));
733 /* Validate iface is online and supported. */
734 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
735 printf("%s: interface '%s' does not support debugnet\n",
736 __func__, if_name(ifp));
740 if ((if_getflags(ifp) & IFF_UP) == 0) {
741 printf("%s: interface '%s' link is down\n", __func__,
747 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
748 pcb->dp_event_started = true;
751 * We maintain the invariant that g_debugnet_pcb_inuse is always true
752 * while the debugnet ifp's if_input is overridden with
755 g_debugnet_pcb_inuse = true;
757 /* Make the card use *our* receive callback. */
758 pcb->dp_drv_input = ifp->if_input;
759 ifp->if_input = debugnet_input;
761 printf("%s: searching for %s MAC...\n", __func__,
762 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
764 error = debugnet_arp_gw(pcb);
766 printf("%s: failed to locate MAC address\n", __func__);
769 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
771 herald_auxdata = (struct debugnet_proto_aux) {
772 .dp_offset_start = dcp->dc_herald_offset,
773 .dp_aux2 = dcp->dc_herald_aux2,
775 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
776 dcp->dc_herald_datalen, &herald_auxdata);
778 printf("%s: failed to herald debugnet server\n", __func__);
791 * Pre-allocated dump-time mbuf tracking.
793 * We just track the high water mark we've ever seen and allocate appropriately
794 * for that iface/mtu combo.
801 static struct mtx dn_hwm_lk;
802 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
805 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
810 mtx_lock(&dn_hwm_lk);
812 if (nmbuf > dn_hwm.nmbuf) {
814 dn_hwm.nmbuf = nmbuf;
816 nmbuf = dn_hwm.nmbuf;
818 if (ncl > dn_hwm.ncl) {
824 if (clsize > dn_hwm.clsize) {
826 dn_hwm.clsize = clsize;
828 clsize = dn_hwm.clsize;
830 mtx_unlock(&dn_hwm_lk);
833 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
837 debugnet_any_ifnet_update(struct ifnet *ifp)
839 int clsize, nmbuf, ncl, nrxr;
841 if (!DEBUGNET_SUPPORTED_NIC(ifp))
844 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
845 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
848 * We need two headers per message on the transmit side. Multiply by
849 * four to give us some breathing room.
851 nmbuf = ncl * (4 + nrxr);
855 * Bandaid for drivers that (incorrectly) advertise LinkUp before their
856 * dn_init method is available.
858 if (nmbuf == 0 || ncl == 0 || clsize == 0) {
862 printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
863 __func__, if_name(ifp), ifp);
866 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
870 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
871 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
873 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
874 * because the driver is still in attach. Since we cannot use down interfaces,
875 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
876 * with vtnet and dhcpclient that event just never occurs.
878 * So that's how I've landed on the lower level ifnet_link_event.
882 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
884 if (link_state == LINK_STATE_UP)
885 debugnet_any_ifnet_update(ifp);
888 static eventhandler_tag dn_attach_cookie;
890 dn_evh_init(void *ctx __unused)
892 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
893 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
895 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
898 * DDB parsing helpers for debugnet(4) consumers.
903 const char *printname;
908 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
915 for (octet = 0; octet < 4; octet++) {
916 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
918 db_printf("%s:%s: octet %u expected number; found %d\n",
919 __func__, opt->printname, octet, t);
923 * db_lex lexes '-' distinctly from the number itself, but
924 * let's document that invariant.
926 MPASS(db_tok_number >= 0);
928 if (db_tok_number > UINT8_MAX) {
929 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
930 opt->printname, octet, (intmax_t)db_tok_number);
934 /* Constructed host-endian and converted to network later. */
935 tmp = (tmp << 8) | db_tok_number;
938 t = db_read_token_flags(DRT_WSPACE);
940 db_printf("%s:%s: octet %u expected '.'; found"
941 " %d\n", __func__, opt->printname, octet,
948 *opt->result = htonl(tmp);
954 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
961 struct my_inet_opt opt_client = {
962 .printname = "client",
963 .result = &result->dd_client,
966 .printname = "server",
967 .result = &result->dd_server,
970 .printname = "gateway",
971 .result = &result->dd_gateway,
976 memset(result, 0, sizeof(*result));
979 * command [space] [-] [opt] [[space] [optarg]] ...
981 * db_command has already lexed 'command' for us.
983 t = db_read_token_flags(DRT_WSPACE);
985 t = db_read_token_flags(DRT_WSPACE);
989 db_printf("%s: Bad syntax; expected '-', got %d\n",
994 t = db_read_token_flags(DRT_WSPACE);
996 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
1001 if (strlen(db_tok_string) > 1) {
1002 db_printf("%s: Bad syntax; expected single option "
1003 "flag, got '%s'\n", cmd, db_tok_string);
1008 cur_inet_opt = NULL;
1009 switch ((ch = db_tok_string[0])) {
1011 DNETDEBUG("Unexpected: '%c'\n", ch);
1016 cur_inet_opt = &opt_client;
1019 cur_inet_opt = &opt_gateway;
1022 cur_inet_opt = &opt_server;
1029 t = db_read_token_flags(DRT_WSPACE);
1031 db_printf("%s: Bad syntax; expected space after "
1032 "flag %c, got %d\n", cmd, ch, t);
1037 t = db_read_token_flags(DRT_WSPACE);
1039 db_printf("%s: Expected interface but got %d\n",
1046 * We *don't* take a ref here because the only current
1047 * consumer, db_netdump_cmd, does not need it. It
1048 * (somewhat redundantly) extracts the if_name(),
1049 * re-lookups the ifp, and takes its own reference.
1051 ifp = ifunit(db_tok_string);
1054 db_printf("Could not locate interface %s\n",
1060 MPASS(cur_inet_opt != NULL);
1061 /* Assume IPv4 for now. */
1062 error = dn_parse_optarg_ipv4(cur_inet_opt);
1067 /* Skip (mandatory) whitespace after option, if not EOL. */
1068 t = db_read_token_flags(DRT_WSPACE);
1072 db_printf("%s: Bad syntax; expected space after "
1073 "flag %c option; got %d\n", cmd, ch, t);
1076 t = db_read_token_flags(DRT_WSPACE);
1079 if (!opt_server.has_opt) {
1080 db_printf("%s: need a destination server address\n", cmd);
1084 result->dd_has_client = opt_client.has_opt;
1085 result->dd_has_gateway = opt_gateway.has_opt;
1086 result->dd_ifp = ifp;
1088 /* We parsed the full line to tEOL already, or bailed with an error. */
1092 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1093 "-i <interface>]\n", cmd);