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>
60 #include <net/route.h>
61 #include <net/route/nhop.h>
63 #include <netinet/in.h>
64 #include <netinet/in_fib.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/ip_options.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
73 #include <machine/in_cksum.h>
74 #include <machine/pcb.h>
76 #include <net/debugnet.h>
77 #define DEBUGNET_INTERNAL
78 #include <net/debugnet_int.h>
80 FEATURE(debugnet, "Debugnet support");
82 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
83 "debugnet parameters");
85 unsigned debugnet_debug;
86 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
88 "Debug message verbosity (0: off; 1: on; 2: verbose)");
90 int debugnet_npolls = 2000;
91 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
93 "Number of times to poll before assuming packet loss (0.5ms per poll)");
94 int debugnet_nretries = 10;
95 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
96 &debugnet_nretries, 0,
97 "Number of retransmit attempts before giving up");
98 int debugnet_fib = RT_DEFAULT_FIB;
99 SYSCTL_INT(_net_debugnet, OID_AUTO, fib, CTLFLAG_RWTUN,
101 "Fib to use when sending dump");
103 static bool g_debugnet_pcb_inuse;
104 static struct debugnet_pcb g_dnet_pcb;
107 * Simple accessors for opaque PCB.
109 const unsigned char *
110 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
112 MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
113 pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
114 return (pcb->dp_gw_mac.octet);
118 * Start of network primitives, beginning with output primitives.
122 * Handles creation of the ethernet header, then places outgoing packets into
123 * the tx buffer for the NIC
126 * m The mbuf containing the packet to be sent (will be freed by
127 * this function or the NIC driver)
128 * ifp The interface to send on
129 * dst The destination ethernet address (source address will be looked
131 * etype The ETHERTYPE_* value for the protocol that is being sent
134 * int see errno.h, 0 for success
137 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
140 struct ether_header *eh;
142 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
143 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
144 if_printf(ifp, "%s: interface isn't up\n", __func__);
149 /* Fill in the ethernet header. */
150 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
152 printf("%s: out of mbufs\n", __func__);
155 eh = mtod(m, struct ether_header *);
156 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
157 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
158 eh->ether_type = htons(etype);
159 return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
163 * Unreliable transmission of an mbuf chain to the debugnet server
164 * Note: can't handle fragmentation; fails if the packet is larger than
165 * ifp->if_mtu after adding the UDP/IP headers
168 * pcb The debugnet context block
172 * int see errno.h, 0 for success
175 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
179 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
181 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
183 printf("%s: out of mbufs\n", __func__);
187 udp = mtod(m, void *);
188 udp->uh_ulen = htons(m->m_pkthdr.len);
189 /* Use this src port so that the server can connect() the socket */
190 udp->uh_sport = htons(pcb->dp_client_port);
191 udp->uh_dport = htons(pcb->dp_server_port);
192 /* Computed later (protocol-dependent). */
195 return (debugnet_ip_output(pcb, m));
199 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
201 struct debugnet_ack *dn_ack;
204 DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
206 m = m_gethdr(M_NOWAIT, MT_DATA);
208 printf("%s: Out of mbufs\n", __func__);
211 m->m_len = sizeof(*dn_ack);
212 m->m_pkthdr.len = sizeof(*dn_ack);
213 MH_ALIGN(m, sizeof(*dn_ack));
214 dn_ack = mtod(m, void *);
215 dn_ack->da_seqno = seqno;
217 return (debugnet_udp_output(pcb, m));
221 * Dummy free function for debugnet clusters.
224 debugnet_mbuf_free(struct mbuf *m __unused)
229 * Construct and reliably send a debugnet packet. May fail from a resource
230 * shortage or extreme number of unacknowledged retransmissions. Wait for
231 * an acknowledgement before returning. Splits packets into chunks small
232 * enough to be sent without fragmentation (looks up the interface MTU)
235 * type debugnet packet type (HERALD, FINISHED, ...)
237 * datalen data size (bytes)
238 * auxdata optional auxiliary information
241 * int see errno.h, 0 for success
244 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
245 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
247 struct debugnet_msg_hdr *dn_msg_hdr;
250 uint32_t i, pktlen, sent_so_far;
251 int retries, polls, error;
253 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
257 pcb->dp_rcvd_acks = 0;
261 /* Chunks can be too big to fit in packets. */
262 for (i = sent_so_far = 0; sent_so_far < datalen ||
263 (i == 0 && datalen == 0); i++) {
264 pktlen = datalen - sent_so_far;
266 /* Bound: the interface MTU (assume no IP options). */
267 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
268 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
271 * Check if it is retransmitting and this has been ACKed
274 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
275 sent_so_far += pktlen;
280 * Get and fill a header mbuf, then chain data as an extended
283 m = m_gethdr(M_NOWAIT, MT_DATA);
285 printf("%s: Out of mbufs\n", __func__);
288 m->m_len = sizeof(struct debugnet_msg_hdr);
289 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
290 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
291 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
292 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
293 dn_msg_hdr->mh_type = htonl(type);
294 dn_msg_hdr->mh_len = htonl(pktlen);
296 if (auxdata != NULL) {
297 dn_msg_hdr->mh_offset =
298 htobe64(auxdata->dp_offset_start + sent_so_far);
299 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
301 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
302 dn_msg_hdr->mh_aux2 = 0;
306 m2 = m_get(M_NOWAIT, MT_DATA);
309 printf("%s: Out of mbufs\n", __func__);
312 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
313 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
318 m->m_pkthdr.len += pktlen;
320 error = debugnet_udp_output(pcb, m);
324 /* Note that we're waiting for this packet in the bitfield. */
325 want_acks |= (1 << i);
326 sent_so_far += pktlen;
328 if (i >= DEBUGNET_MAX_IN_FLIGHT)
329 printf("Warning: Sent more than %d packets (%d). "
330 "Acknowledgements will fail unless the size of "
331 "rcvd_acks/want_acks is increased.\n",
332 DEBUGNET_MAX_IN_FLIGHT, i);
335 * Wait for acks. A *real* window would speed things up considerably.
338 while (pcb->dp_rcvd_acks != want_acks) {
339 if (polls++ > debugnet_npolls) {
340 if (retries++ > debugnet_nretries)
345 debugnet_network_poll(pcb);
347 if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
355 * Network input primitives.
359 * Just introspect the header enough to fire off a seqno ack and validate
363 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
365 const struct debugnet_msg_hdr *dnh;
371 if (m->m_pkthdr.len < sizeof(*dnh)) {
372 DNETDEBUG("ignoring small debugnet_msg packet\n");
377 if (m->m_len < sizeof(*dnh)) {
378 m = m_pullup(m, sizeof(*dnh));
381 DNETDEBUG("m_pullup failed\n");
385 dnh = mtod(m, const void *);
387 if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
388 DNETDEBUG("Dropping short packet.\n");
393 * If the issue is transient (ENOBUFS), sender should resend. If
394 * non-transient (like driver objecting to rx -> tx from the same
395 * thread), not much else we can do.
397 error = debugnet_ack_output(pcb, dnh->mh_seqno);
401 if (ntohl(dnh->mh_type) == DEBUGNET_FINISHED) {
402 printf("Remote shut down the connection on us!\n");
403 pcb->dp_state = DN_STATE_REMOTE_CLOSED;
406 * Continue through to the user handler so they are signalled
407 * not to wait for further rx.
411 pcb->dp_rx_handler(pcb, mb);
415 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
417 const struct debugnet_ack *dn_ack;
424 if (m->m_len < sizeof(*dn_ack)) {
425 m = m_pullup(m, sizeof(*dn_ack));
428 DNETDEBUG("m_pullup failed\n");
432 dn_ack = mtod(m, const void *);
434 /* Debugnet processing. */
436 * Packet is meant for us. Extract the ack sequence number and the
437 * port number if necessary.
439 rcv_ackno = ntohl(dn_ack->da_seqno);
440 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
441 pcb->dp_server_port = sport;
442 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
444 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
445 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
446 else if (rcv_ackno >= pcb->dp_seqno) {
447 /* We're interested in this ack. Record it. */
448 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
453 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
455 const struct udphdr *udp;
457 uint16_t sport, ulen;
459 /* UDP processing. */
462 if (m->m_pkthdr.len < sizeof(*udp)) {
463 DNETDEBUG("ignoring small UDP packet\n");
467 /* Get UDP headers. */
468 if (m->m_len < sizeof(*udp)) {
469 m = m_pullup(m, sizeof(*udp));
472 DNETDEBUG("m_pullup failed\n");
476 udp = mtod(m, const void *);
478 /* We expect to receive UDP packets on the configured client port. */
479 if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
480 DNETDEBUG("not on the expected port.\n");
484 /* Check that ulen does not exceed actual size of data. */
485 ulen = ntohs(udp->uh_ulen);
486 if (m->m_pkthdr.len < ulen) {
487 DNETDEBUG("ignoring runt UDP packet\n");
491 sport = ntohs(udp->uh_sport);
493 m_adj(m, sizeof(*udp));
494 ulen -= sizeof(*udp);
496 if (ulen == sizeof(struct debugnet_ack)) {
497 debugnet_handle_ack(pcb, mb, sport);
501 if (pcb->dp_rx_handler == NULL) {
502 if (ulen < sizeof(struct debugnet_ack))
503 DNETDEBUG("ignoring small ACK packet\n");
505 DNETDEBUG("ignoring unexpected non-ACK packet on "
506 "half-duplex connection.\n");
510 debugnet_handle_rx_msg(pcb, mb);
514 * Handler for incoming packets directly from the network adapter
515 * Identifies the packet type (IP or ARP) and passes it along to one of the
516 * helper functions debugnet_handle_ip or debugnet_handle_arp.
518 * It needs to partially replicate the behaviour of ether_input() and
522 * ifp the interface the packet came from
523 * m an mbuf containing the packet received
526 debugnet_input_one(struct ifnet *ifp, struct mbuf *m)
529 struct ether_header *eh;
532 /* Ethernet processing. */
533 if ((m->m_flags & M_PKTHDR) == 0) {
534 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
537 if (m->m_len < ETHER_HDR_LEN) {
539 "discard frame without leading eth header (len %u pktlen %u)\n",
540 m->m_len, m->m_pkthdr.len);
543 if ((m->m_flags & M_HASFCS) != 0) {
544 m_adj(m, -ETHER_CRC_LEN);
545 m->m_flags &= ~M_HASFCS;
547 eh = mtod(m, struct ether_header *);
548 etype = ntohs(eh->ether_type);
549 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
550 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
553 if (if_gethwaddr(ifp, &ifr) != 0) {
554 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
557 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
558 ETHER_ADDR_LEN) != 0 &&
559 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
561 "discard frame with incorrect destination addr\n");
565 MPASS(g_debugnet_pcb_inuse);
567 /* Done ethernet processing. Strip off the ethernet header. */
568 m_adj(m, ETHER_HDR_LEN);
571 debugnet_handle_arp(&g_dnet_pcb, &m);
574 debugnet_handle_ip(&g_dnet_pcb, &m);
577 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
586 debugnet_input(struct ifnet *ifp, struct mbuf *m)
593 debugnet_input_one(ifp, m);
599 * Network polling primitive.
601 * Instead of assuming that most of the network stack is sane, we just poll the
602 * driver directly for packets.
605 debugnet_network_poll(struct debugnet_pcb *pcb)
610 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
614 * Start of consumer API surface.
617 debugnet_free(struct debugnet_pcb *pcb)
621 MPASS(pcb == &g_dnet_pcb);
622 MPASS(pcb->dp_drv_input == NULL || g_debugnet_pcb_inuse);
626 if (pcb->dp_drv_input != NULL)
627 ifp->if_input = pcb->dp_drv_input;
628 if (pcb->dp_event_started)
629 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
631 debugnet_mbuf_finish();
633 g_debugnet_pcb_inuse = false;
634 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
638 debugnet_connect(const struct debugnet_conn_params *dcp,
639 struct debugnet_pcb **pcb_out)
641 struct debugnet_proto_aux herald_auxdata;
642 struct debugnet_pcb *pcb;
646 if (g_debugnet_pcb_inuse) {
647 printf("%s: Only one connection at a time.\n", __func__);
652 *pcb = (struct debugnet_pcb) {
653 .dp_state = DN_STATE_INIT,
654 .dp_client = dcp->dc_client,
655 .dp_server = dcp->dc_server,
656 .dp_gateway = dcp->dc_gateway,
657 .dp_server_port = dcp->dc_herald_port, /* Initially */
658 .dp_client_port = dcp->dc_client_port,
660 .dp_ifp = dcp->dc_ifp,
661 .dp_rx_handler = dcp->dc_rx_handler,
662 .dp_drv_input = NULL,
665 /* Switch to the debugnet mbuf zones. */
666 debugnet_mbuf_start();
668 /* At least one needed parameter is missing; infer it. */
669 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
670 pcb->dp_ifp == NULL) {
671 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
672 struct ifnet *rt_ifp;
673 struct nhop_object *nh;
675 memset(&dest_sin, 0, sizeof(dest_sin));
676 dest_sin = (struct sockaddr_in) {
677 .sin_len = sizeof(dest_sin),
678 .sin_family = AF_INET,
679 .sin_addr.s_addr = pcb->dp_server,
683 nh = fib4_lookup_debugnet(debugnet_fib, dest_sin.sin_addr, 0,
688 printf("%s: Could not get route for that server.\n",
694 /* TODO support AF_INET6 */
695 if (nh->gw_sa.sa_family == AF_INET)
696 gw_sin = &nh->gw4_sa;
698 if (nh->gw_sa.sa_family == AF_LINK)
699 DNETDEBUG("Destination address is on link.\n");
703 MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
704 local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
708 if (pcb->dp_client == INADDR_ANY)
709 pcb->dp_client = local_sin->sin_addr.s_addr;
710 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
711 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
712 if (pcb->dp_ifp == NULL)
713 pcb->dp_ifp = rt_ifp;
718 if (debugnet_debug > 0) {
719 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
720 gwbuf[INET_ADDRSTRLEN];
721 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
722 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
723 if (pcb->dp_gateway != INADDR_ANY)
724 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
725 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
726 serbuf, pcb->dp_server_port,
727 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
728 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
729 clibuf, pcb->dp_client_port, if_name(ifp));
732 /* Validate iface is online and supported. */
733 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
734 printf("%s: interface '%s' does not support debugnet\n",
735 __func__, if_name(ifp));
739 if ((if_getflags(ifp) & IFF_UP) == 0) {
740 printf("%s: interface '%s' link is down\n", __func__,
746 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
747 pcb->dp_event_started = true;
750 * We maintain the invariant that g_debugnet_pcb_inuse is always true
751 * while the debugnet ifp's if_input is overridden with
754 g_debugnet_pcb_inuse = true;
756 /* Make the card use *our* receive callback. */
757 pcb->dp_drv_input = ifp->if_input;
758 ifp->if_input = debugnet_input;
760 printf("%s: searching for %s MAC...\n", __func__,
761 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
763 error = debugnet_arp_gw(pcb);
765 printf("%s: failed to locate MAC address\n", __func__);
768 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
770 herald_auxdata = (struct debugnet_proto_aux) {
771 .dp_offset_start = dcp->dc_herald_offset,
772 .dp_aux2 = dcp->dc_herald_aux2,
774 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
775 dcp->dc_herald_datalen, &herald_auxdata);
777 printf("%s: failed to herald debugnet server\n", __func__);
790 * Pre-allocated dump-time mbuf tracking.
792 * We just track the high water mark we've ever seen and allocate appropriately
793 * for that iface/mtu combo.
800 static struct mtx dn_hwm_lk;
801 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
804 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
809 mtx_lock(&dn_hwm_lk);
811 if (nmbuf > dn_hwm.nmbuf) {
813 dn_hwm.nmbuf = nmbuf;
815 nmbuf = dn_hwm.nmbuf;
817 if (ncl > dn_hwm.ncl) {
823 if (clsize > dn_hwm.clsize) {
825 dn_hwm.clsize = clsize;
827 clsize = dn_hwm.clsize;
829 mtx_unlock(&dn_hwm_lk);
832 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
836 debugnet_any_ifnet_update(struct ifnet *ifp)
838 int clsize, nmbuf, ncl, nrxr;
840 if (!DEBUGNET_SUPPORTED_NIC(ifp))
843 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
844 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
847 * We need two headers per message on the transmit side. Multiply by
848 * four to give us some breathing room.
850 nmbuf = ncl * (4 + nrxr);
854 * Bandaid for drivers that (incorrectly) advertise LinkUp before their
855 * dn_init method is available.
857 if (nmbuf == 0 || ncl == 0 || clsize == 0) {
858 printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
859 __func__, if_name(ifp), ifp);
862 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
866 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
867 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
869 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
870 * because the driver is still in attach. Since we cannot use down interfaces,
871 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
872 * with vtnet and dhcpclient that event just never occurs.
874 * So that's how I've landed on the lower level ifnet_link_event.
878 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
880 if (link_state == LINK_STATE_UP)
881 debugnet_any_ifnet_update(ifp);
884 static eventhandler_tag dn_attach_cookie;
886 dn_evh_init(void *ctx __unused)
888 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
889 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
891 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
894 * DDB parsing helpers for debugnet(4) consumers.
899 const char *printname;
904 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
911 for (octet = 0; octet < 4; octet++) {
912 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
914 db_printf("%s:%s: octet %u expected number; found %d\n",
915 __func__, opt->printname, octet, t);
919 * db_lex lexes '-' distinctly from the number itself, but
920 * let's document that invariant.
922 MPASS(db_tok_number >= 0);
924 if (db_tok_number > UINT8_MAX) {
925 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
926 opt->printname, octet, (intmax_t)db_tok_number);
930 /* Constructed host-endian and converted to network later. */
931 tmp = (tmp << 8) | db_tok_number;
934 t = db_read_token_flags(DRT_WSPACE);
936 db_printf("%s:%s: octet %u expected '.'; found"
937 " %d\n", __func__, opt->printname, octet,
944 *opt->result = htonl(tmp);
950 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
957 struct my_inet_opt opt_client = {
958 .printname = "client",
959 .result = &result->dd_client,
962 .printname = "server",
963 .result = &result->dd_server,
966 .printname = "gateway",
967 .result = &result->dd_gateway,
972 memset(result, 0, sizeof(*result));
975 * command [space] [-] [opt] [[space] [optarg]] ...
977 * db_command has already lexed 'command' for us.
979 t = db_read_token_flags(DRT_WSPACE);
981 t = db_read_token_flags(DRT_WSPACE);
985 db_printf("%s: Bad syntax; expected '-', got %d\n",
990 t = db_read_token_flags(DRT_WSPACE);
992 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
997 if (strlen(db_tok_string) > 1) {
998 db_printf("%s: Bad syntax; expected single option "
999 "flag, got '%s'\n", cmd, db_tok_string);
1004 cur_inet_opt = NULL;
1005 switch ((ch = db_tok_string[0])) {
1007 DNETDEBUG("Unexpected: '%c'\n", ch);
1012 cur_inet_opt = &opt_client;
1015 cur_inet_opt = &opt_gateway;
1018 cur_inet_opt = &opt_server;
1025 t = db_read_token_flags(DRT_WSPACE);
1027 db_printf("%s: Bad syntax; expected space after "
1028 "flag %c, got %d\n", cmd, ch, t);
1033 t = db_read_token_flags(DRT_WSPACE);
1035 db_printf("%s: Expected interface but got %d\n",
1042 * We *don't* take a ref here because the only current
1043 * consumer, db_netdump_cmd, does not need it. It
1044 * (somewhat redundantly) extracts the if_name(),
1045 * re-lookups the ifp, and takes its own reference.
1047 ifp = ifunit(db_tok_string);
1050 db_printf("Could not locate interface %s\n",
1056 MPASS(cur_inet_opt != NULL);
1057 /* Assume IPv4 for now. */
1058 error = dn_parse_optarg_ipv4(cur_inet_opt);
1063 /* Skip (mandatory) whitespace after option, if not EOL. */
1064 t = db_read_token_flags(DRT_WSPACE);
1068 db_printf("%s: Bad syntax; expected space after "
1069 "flag %c option; got %d\n", cmd, ch, t);
1072 t = db_read_token_flags(DRT_WSPACE);
1075 if (!opt_server.has_opt) {
1076 db_printf("%s: need a destination server address\n", cmd);
1080 result->dd_has_client = opt_client.has_opt;
1081 result->dd_has_gateway = opt_gateway.has_opt;
1082 result->dd_ifp = ifp;
1084 /* We parsed the full line to tEOL already, or bailed with an error. */
1088 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1089 "-i <interface>]\n", cmd);