2 * Copyright (c) 2010-2012 Citrix Inc.
3 * Copyright (c) 2009-2012 Microsoft Corp.
4 * Copyright (c) 2012 NetApp Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2004-2006 Kip Macy
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include "opt_inet6.h"
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
65 #include <sys/malloc.h>
66 #include <sys/module.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69 #include <sys/queue.h>
72 #include <sys/sysctl.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
82 #include <net/if_var.h>
83 #include <net/if_types.h>
84 #include <net/if_vlan_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/in.h>
88 #include <netinet/ip.h>
89 #include <netinet/if_ether.h>
90 #include <netinet/tcp.h>
91 #include <netinet/udp.h>
92 #include <netinet/ip6.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_kern.h>
99 #include <machine/bus.h>
100 #include <machine/resource.h>
101 #include <machine/frame.h>
102 #include <machine/vmparam.h>
105 #include <sys/rman.h>
106 #include <sys/mutex.h>
107 #include <sys/errno.h>
108 #include <sys/types.h>
109 #include <machine/atomic.h>
111 #include <machine/intr_machdep.h>
113 #include <machine/in_cksum.h>
115 #include <dev/hyperv/include/hyperv.h>
116 #include "hv_net_vsc.h"
117 #include "hv_rndis.h"
118 #include "hv_rndis_filter.h"
121 /* Short for Hyper-V network interface */
122 #define NETVSC_DEVNAME "hn"
125 * It looks like offset 0 of buf is reserved to hold the softc pointer.
126 * The sc pointer evidently not needed, and is not presently populated.
127 * The packet offset is where the netvsc_packet starts in the buffer.
129 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
130 #define HV_NV_PACKET_OFFSET_IN_BUF 16
133 * A unified flag for all outbound check sum flags is useful,
134 * and it helps avoiding unnecessary check sum calculation in
135 * network forwarding scenario.
137 #define HV_CSUM_FOR_OUTBOUND \
138 (CSUM_IP|CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP|CSUM_IP_TSO| \
139 CSUM_IP_ISCSI|CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP| \
140 CSUM_IP6_TSO|CSUM_IP6_ISCSI)
142 /* XXX move to netinet/tcp_lro.h */
143 #define HN_LRO_HIWAT_MAX 65535
144 #define HN_LRO_HIWAT_DEF HN_LRO_HIWAT_MAX
145 /* YYY 2*MTU is a bit rough, but should be good enough. */
146 #define HN_LRO_HIWAT_MTULIM(ifp) (2 * (ifp)->if_mtu)
147 #define HN_LRO_HIWAT_ISVALID(sc, hiwat) \
148 ((hiwat) >= HN_LRO_HIWAT_MTULIM((sc)->hn_ifp) || \
149 (hiwat) <= HN_LRO_HIWAT_MAX)
152 * Be aware that this sleepable mutex will exhibit WITNESS errors when
153 * certain TCP and ARP code paths are taken. This appears to be a
154 * well-known condition, as all other drivers checked use a sleeping
155 * mutex to protect their transmit paths.
156 * Also Be aware that mutexes do not play well with semaphores, and there
157 * is a conflicting semaphore in a certain channel code path.
159 #define NV_LOCK_INIT(_sc, _name) \
160 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
161 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
162 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
163 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
164 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
171 int hv_promisc_mode = 0; /* normal mode by default */
173 /* Trust tcp segements verification on host side. */
174 static int hn_trust_hosttcp = 0;
175 TUNABLE_INT("dev.hn.trust_hosttcp", &hn_trust_hosttcp);
178 * Forward declarations
180 static void hn_stop(hn_softc_t *sc);
181 static void hn_ifinit_locked(hn_softc_t *sc);
182 static void hn_ifinit(void *xsc);
183 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
184 static int hn_start_locked(struct ifnet *ifp);
185 static void hn_start(struct ifnet *ifp);
186 static int hn_ifmedia_upd(struct ifnet *ifp);
187 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
189 static int hn_lro_hiwat_sysctl(SYSCTL_HANDLER_ARGS);
191 static int hn_check_iplen(const struct mbuf *, int);
194 hn_set_lro_hiwat(struct hn_softc *sc, int hiwat)
196 sc->hn_lro_hiwat = hiwat;
198 sc->hn_lro.lro_hiwat = sc->hn_lro_hiwat;
203 * NetVsc get message transport protocol type
205 static uint32_t get_transport_proto_type(struct mbuf *m_head)
207 uint32_t ret_val = TRANSPORT_TYPE_NOT_IP;
208 uint16_t ether_type = 0;
210 struct ether_vlan_header *eh;
218 eh = mtod(m_head, struct ether_vlan_header*);
219 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
220 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
221 ether_type = eh->evl_proto;
223 ether_len = ETHER_HDR_LEN;
224 ether_type = eh->evl_encap_proto;
227 switch (ntohs(ether_type)) {
230 ip6 = (struct ip6_hdr *)(m_head->m_data + ether_len);
232 if (IPPROTO_TCP == ip6->ip6_nxt) {
233 ret_val = TRANSPORT_TYPE_IPV6_TCP;
234 } else if (IPPROTO_UDP == ip6->ip6_nxt) {
235 ret_val = TRANSPORT_TYPE_IPV6_UDP;
241 iph = (struct ip *)(m_head->m_data + ether_len);
243 if (IPPROTO_TCP == iph->ip_p) {
244 ret_val = TRANSPORT_TYPE_IPV4_TCP;
245 } else if (IPPROTO_UDP == iph->ip_p) {
246 ret_val = TRANSPORT_TYPE_IPV4_UDP;
251 ret_val = TRANSPORT_TYPE_NOT_IP;
259 hn_ifmedia_upd(struct ifnet *ifp __unused)
266 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
268 struct hn_softc *sc = ifp->if_softc;
270 ifmr->ifm_status = IFM_AVALID;
271 ifmr->ifm_active = IFM_ETHER;
273 if (!sc->hn_carrier) {
274 ifmr->ifm_active |= IFM_NONE;
277 ifmr->ifm_status |= IFM_ACTIVE;
278 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
281 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
282 static const hv_guid g_net_vsc_device_type = {
283 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
284 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
288 * Standard probe entry point.
292 netvsc_probe(device_t dev)
296 p = vmbus_get_type(dev);
297 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
298 device_set_desc(dev, "Synthetic Network Interface");
300 printf("Netvsc probe... DONE \n");
302 return (BUS_PROBE_DEFAULT);
309 * Standard attach entry point.
311 * Called when the driver is loaded. It allocates needed resources,
312 * and initializes the "hardware" and software.
315 netvsc_attach(device_t dev)
317 struct hv_device *device_ctx = vmbus_get_devctx(dev);
318 netvsc_device_info device_info;
320 int unit = device_get_unit(dev);
322 struct sysctl_oid_list *child;
323 struct sysctl_ctx_list *ctx;
326 sc = device_get_softc(dev);
331 bzero(sc, sizeof(hn_softc_t));
334 sc->hn_lro_hiwat = HN_LRO_HIWAT_DEF;
335 sc->hn_trust_hosttcp = hn_trust_hosttcp;
337 NV_LOCK_INIT(sc, "NetVSCLock");
339 sc->hn_dev_obj = device_ctx;
341 ifp = sc->hn_ifp = if_alloc(IFT_ETHER);
344 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
345 ifp->if_dunit = unit;
346 ifp->if_dname = NETVSC_DEVNAME;
348 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
349 ifp->if_ioctl = hn_ioctl;
350 ifp->if_start = hn_start;
351 ifp->if_init = hn_ifinit;
352 /* needed by hv_rf_on_device_add() code */
353 ifp->if_mtu = ETHERMTU;
354 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
355 ifp->if_snd.ifq_drv_maxlen = 511;
356 IFQ_SET_READY(&ifp->if_snd);
358 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
359 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
360 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
361 /* XXX ifmedia_set really should do this for us */
362 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
365 * Tell upper layers that we support full VLAN capability.
367 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
368 ifp->if_capabilities |=
369 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
372 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
375 * Only enable UDP checksum offloading when it is on 2012R2 or
376 * later. UDP checksum offloading doesn't work on earlier
379 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
380 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
382 ifp->if_hwassist = CSUM_TCP | CSUM_TSO;
384 ret = hv_rf_on_device_add(device_ctx, &device_info);
390 if (device_info.link_state == 0) {
394 tcp_lro_init(&sc->hn_lro);
395 /* Driver private LRO settings */
396 sc->hn_lro.ifp = ifp;
398 sc->hn_lro.lro_hiwat = sc->hn_lro_hiwat;
401 ether_ifattach(ifp, device_info.mac_addr);
403 ctx = device_get_sysctl_ctx(dev);
404 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
406 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "lro_queued",
407 CTLFLAG_RW, &sc->hn_lro.lro_queued, 0, "LRO queued");
408 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "lro_flushed",
409 CTLFLAG_RW, &sc->hn_lro.lro_flushed, 0, "LRO flushed");
410 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "lro_tried",
411 CTLFLAG_RW, &sc->hn_lro_tried, "# of LRO tries");
413 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_hiwat",
414 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_hiwat_sysctl,
415 "I", "LRO high watermark");
417 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "trust_hosttcp",
418 CTLFLAG_RW, &sc->hn_trust_hosttcp, 0,
419 "Trust tcp segement verification on host side, "
420 "when csum info is missing");
421 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_ip",
422 CTLFLAG_RW, &sc->hn_csum_ip, "RXCSUM IP");
423 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_tcp",
424 CTLFLAG_RW, &sc->hn_csum_tcp, "RXCSUM TCP");
425 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_trusted",
426 CTLFLAG_RW, &sc->hn_csum_trusted,
427 "# of TCP segements that we trust host's csum verification");
428 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "small_pkts",
429 CTLFLAG_RW, &sc->hn_small_pkts, "# of small packets received");
432 struct sysctl_ctx_list *dc_ctx;
433 struct sysctl_oid_list *dc_child;
437 * Add sysctl nodes for devclass
439 dc = device_get_devclass(dev);
440 dc_ctx = devclass_get_sysctl_ctx(dc);
441 dc_child = SYSCTL_CHILDREN(devclass_get_sysctl_tree(dc));
443 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "trust_hosttcp",
444 CTLFLAG_RD, &hn_trust_hosttcp, 0,
445 "Trust tcp segement verification on host side, "
446 "when csum info is missing (global setting)");
453 * Standard detach entry point
456 netvsc_detach(device_t dev)
458 struct hn_softc *sc = device_get_softc(dev);
459 struct hv_device *hv_device = vmbus_get_devctx(dev);
462 printf("netvsc_detach\n");
465 * XXXKYS: Need to clean up all our
466 * driver state; this is the driver
471 * XXXKYS: Need to stop outgoing traffic and unregister
475 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
477 ifmedia_removeall(&sc->hn_media);
478 tcp_lro_free(&sc->hn_lro);
484 * Standard shutdown entry point
487 netvsc_shutdown(device_t dev)
493 * Send completion processing
495 * Note: It looks like offset 0 of buf is reserved to hold the softc
496 * pointer. The sc pointer is not currently needed in this function, and
497 * it is not presently populated by the TX function.
500 netvsc_xmit_completion(void *context)
502 netvsc_packet *packet = (netvsc_packet *)context;
506 mb = (struct mbuf *)(uintptr_t)packet->compl.send.send_completion_tid;
507 buf = ((uint8_t *)packet) - HV_NV_PACKET_OFFSET_IN_BUF;
517 * Start a transmit of one or more packets
520 hn_start_locked(struct ifnet *ifp)
522 hn_softc_t *sc = ifp->if_softc;
523 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
524 netvsc_dev *net_dev = sc->net_dev;
525 device_t dev = device_ctx->device;
527 netvsc_packet *packet;
528 struct mbuf *m_head, *m;
529 struct mbuf *mc_head = NULL;
530 struct ether_vlan_header *eh;
531 rndis_msg *rndis_mesg;
532 rndis_packet *rndis_pkt;
533 rndis_per_packet_info *rppi;
534 ndis_8021q_info *rppi_vlan_info;
535 rndis_tcp_ip_csum_info *csum_info;
536 rndis_tcp_tso_info *tso_info;
543 uint32_t rndis_msg_size = 0;
544 uint32_t trans_proto_type;
545 uint32_t send_buf_section_idx =
546 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
548 while (!IFQ_DRV_IS_EMPTY(&sc->hn_ifp->if_snd)) {
549 IFQ_DRV_DEQUEUE(&sc->hn_ifp->if_snd, m_head);
550 if (m_head == NULL) {
557 /* Walk the mbuf list computing total length and num frags */
558 for (m = m_head; m != NULL; m = m->m_next) {
566 * Reserve the number of pages requested. Currently,
567 * one page is reserved for the message in the RNDIS
570 num_frags += HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
572 /* If exceeds # page_buffers in netvsc_packet */
573 if (num_frags > NETVSC_PACKET_MAXPAGE) {
574 device_printf(dev, "exceed max page buffers,%d,%d\n",
575 num_frags, NETVSC_PACKET_MAXPAGE);
577 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
582 * Allocate a buffer with space for a netvsc packet plus a
583 * number of reserved areas. First comes a (currently 16
584 * bytes, currently unused) reserved data area. Second is
585 * the netvsc_packet. Third is an area reserved for an
586 * rndis_filter_packet struct. Fourth (optional) is a
587 * rndis_per_packet_info struct.
588 * Changed malloc to M_NOWAIT to avoid sleep under spin lock.
589 * No longer reserving extra space for page buffers, as they
590 * are already part of the netvsc_packet.
592 buf = malloc(HV_NV_PACKET_OFFSET_IN_BUF +
593 sizeof(netvsc_packet) +
595 RNDIS_VLAN_PPI_SIZE +
598 M_NETVSC, M_ZERO | M_NOWAIT);
600 device_printf(dev, "hn:malloc packet failed\n");
602 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
606 packet = (netvsc_packet *)(buf + HV_NV_PACKET_OFFSET_IN_BUF);
607 *(vm_offset_t *)buf = HV_NV_SC_PTR_OFFSET_IN_BUF;
609 packet->is_data_pkt = TRUE;
611 /* Set up the rndis header */
612 packet->page_buf_count = num_frags;
614 /* Initialize it from the mbuf */
615 packet->tot_data_buf_len = len;
618 * extension points to the area reserved for the
619 * rndis_filter_packet, which is placed just after
620 * the netvsc_packet (and rppi struct, if present;
621 * length is updated later).
623 packet->rndis_mesg = packet + 1;
624 rndis_mesg = (rndis_msg *)packet->rndis_mesg;
625 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
627 rndis_pkt = &rndis_mesg->msg.packet;
628 rndis_pkt->data_offset = sizeof(rndis_packet);
629 rndis_pkt->data_length = packet->tot_data_buf_len;
630 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
632 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
635 * If the Hyper-V infrastructure needs to embed a VLAN tag,
636 * initialize netvsc_packet and rppi struct values as needed.
638 if (m_head->m_flags & M_VLANTAG) {
640 * set up some additional fields so the Hyper-V infrastructure will stuff the VLAN tag
643 packet->vlan_tci = m_head->m_pkthdr.ether_vtag;
645 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
647 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
650 /* VLAN info immediately follows rppi struct */
651 rppi_vlan_info = (ndis_8021q_info *)((char*)rppi +
652 rppi->per_packet_info_offset);
653 /* FreeBSD does not support CFI or priority */
654 rppi_vlan_info->u1.s1.vlan_id =
655 packet->vlan_tci & 0xfff;
658 /* Only check the flags for outbound and ignore the ones for inbound */
659 if (0 == (m_head->m_pkthdr.csum_flags & HV_CSUM_FOR_OUTBOUND)) {
663 eh = mtod(m_head, struct ether_vlan_header*);
664 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
665 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
667 ether_len = ETHER_HDR_LEN;
670 trans_proto_type = get_transport_proto_type(m_head);
671 if (TRANSPORT_TYPE_NOT_IP == trans_proto_type) {
676 * TSO packet needless to setup the send side checksum
679 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
683 /* setup checksum offload */
684 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
685 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
687 csum_info = (rndis_tcp_ip_csum_info *)((char*)rppi +
688 rppi->per_packet_info_offset);
690 if (trans_proto_type & (TYPE_IPV4 << 16)) {
691 csum_info->xmit.is_ipv4 = 1;
693 csum_info->xmit.is_ipv6 = 1;
696 if (trans_proto_type & TYPE_TCP) {
697 csum_info->xmit.tcp_csum = 1;
698 csum_info->xmit.tcp_header_offset = 0;
699 } else if (trans_proto_type & TYPE_UDP) {
700 csum_info->xmit.udp_csum = 1;
706 /* setup TCP segmentation offload */
707 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
708 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
709 tcp_large_send_info);
711 tso_info = (rndis_tcp_tso_info *)((char *)rppi +
712 rppi->per_packet_info_offset);
713 tso_info->lso_v2_xmit.type =
714 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
717 if (trans_proto_type & (TYPE_IPV4 << 16)) {
719 (struct ip *)(m_head->m_data + ether_len);
720 unsigned long iph_len = ip->ip_hl << 2;
722 (struct tcphdr *)((caddr_t)ip + iph_len);
724 tso_info->lso_v2_xmit.ip_version =
725 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
729 th->th_sum = in_pseudo(ip->ip_src.s_addr,
734 #if defined(INET6) && defined(INET)
739 struct ip6_hdr *ip6 =
740 (struct ip6_hdr *)(m_head->m_data + ether_len);
741 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
743 tso_info->lso_v2_xmit.ip_version =
744 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
746 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
749 tso_info->lso_v2_xmit.tcp_header_offset = 0;
750 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
753 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
754 packet->tot_data_buf_len = rndis_mesg->msg_len;
756 /* send packet with send buffer */
757 if (packet->tot_data_buf_len < net_dev->send_section_size) {
758 send_buf_section_idx =
759 hv_nv_get_next_send_section(net_dev);
760 if (send_buf_section_idx !=
761 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
762 char *dest = ((char *)net_dev->send_buf +
763 send_buf_section_idx *
764 net_dev->send_section_size);
766 memcpy(dest, rndis_mesg, rndis_msg_size);
767 dest += rndis_msg_size;
768 for (m = m_head; m != NULL; m = m->m_next) {
771 (void *)mtod(m, vm_offset_t),
777 packet->send_buf_section_idx =
778 send_buf_section_idx;
779 packet->send_buf_section_size =
780 packet->tot_data_buf_len;
781 packet->page_buf_count = 0;
786 /* send packet with page buffer */
787 packet->page_buffers[0].pfn =
788 atop(hv_get_phys_addr(rndis_mesg));
789 packet->page_buffers[0].offset =
790 (unsigned long)rndis_mesg & PAGE_MASK;
791 packet->page_buffers[0].length = rndis_msg_size;
794 * Fill the page buffers with mbuf info starting at index
795 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
797 i = HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
798 for (m = m_head; m != NULL; m = m->m_next) {
801 vtophys(mtod(m, vm_offset_t));
802 packet->page_buffers[i].pfn =
804 packet->page_buffers[i].offset =
805 paddr & (PAGE_SIZE - 1);
806 packet->page_buffers[i].length = m->m_len;
811 packet->send_buf_section_idx =
812 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
813 packet->send_buf_section_size = 0;
818 * If bpf, copy the mbuf chain. This is less expensive than
819 * it appears; the mbuf clusters are not copied, only their
820 * reference counts are incremented.
821 * Needed to avoid a race condition where the completion
822 * callback is invoked, freeing the mbuf chain, before the
823 * bpf_mtap code has a chance to run.
826 mc_head = m_copypacket(m_head, M_NOWAIT);
829 /* Set the completion routine */
830 packet->compl.send.on_send_completion = netvsc_xmit_completion;
831 packet->compl.send.send_completion_context = packet;
832 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)m_head;
834 /* Removed critical_enter(), does not appear necessary */
835 ret = hv_nv_on_send(device_ctx, packet);
837 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
838 /* if bpf && mc_head, call bpf_mtap code */
840 ETHER_BPF_MTAP(ifp, mc_head);
848 IF_PREPEND(&ifp->if_snd, m_head);
849 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
852 * Null the mbuf pointer so the completion function
853 * does not free the mbuf chain. We just pushed the
854 * mbuf chain back on the if_snd queue.
856 packet->compl.send.send_completion_tid = 0;
859 * Release the resources since we will not get any
862 netvsc_xmit_completion(packet);
863 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
866 /* if bpf && mc_head, free the mbuf chain copy */
876 * Link up/down notification
879 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
881 hn_softc_t *sc = device_get_softc(device_obj->device);
895 * Append the specified data to the indicated mbuf chain,
896 * Extend the mbuf chain if the new data does not fit in
899 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
900 * There should be an equivalent in the kernel mbuf code,
901 * but there does not appear to be one yet.
903 * Differs from m_append() in that additional mbufs are
904 * allocated with cluster size MJUMPAGESIZE, and filled
907 * Return 1 if able to complete the job; otherwise 0.
910 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
913 int remainder, space;
915 for (m = m0; m->m_next != NULL; m = m->m_next)
918 space = M_TRAILINGSPACE(m);
921 * Copy into available space.
923 if (space > remainder)
925 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
930 while (remainder > 0) {
932 * Allocate a new mbuf; could check space
933 * and allocate a cluster instead.
935 n = m_getjcl(M_NOWAIT, m->m_type, 0, MJUMPAGESIZE);
938 n->m_len = min(MJUMPAGESIZE, remainder);
939 bcopy(cp, mtod(n, caddr_t), n->m_len);
941 remainder -= n->m_len;
945 if (m0->m_flags & M_PKTHDR)
946 m0->m_pkthdr.len += len - remainder;
948 return (remainder == 0);
953 * Called when we receive a data packet from the "wire" on the
956 * Note: This is no longer used as a callback
959 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
960 rndis_tcp_ip_csum_info *csum_info)
962 hn_softc_t *sc = (hn_softc_t *)device_get_softc(device_ctx->device);
965 device_t dev = device_ctx->device;
966 int size, do_lro = 0;
969 return (0); /* TODO: KYS how can this be! */
974 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
979 * Bail out if packet contains more data than configured MTU.
981 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
983 } else if (packet->tot_data_buf_len <= MHLEN) {
984 m_new = m_gethdr(M_NOWAIT, MT_DATA);
987 memcpy(mtod(m_new, void *), packet->data,
988 packet->tot_data_buf_len);
989 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
993 * Get an mbuf with a cluster. For packets 2K or less,
994 * get a standard 2K cluster. For anything larger, get a
995 * 4K cluster. Any buffers larger than 4K can cause problems
996 * if looped around to the Hyper-V TX channel, so avoid them.
999 if (packet->tot_data_buf_len > MCLBYTES) {
1001 size = MJUMPAGESIZE;
1004 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1005 if (m_new == NULL) {
1006 device_printf(dev, "alloc mbuf failed.\n");
1010 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1012 m_new->m_pkthdr.rcvif = ifp;
1014 /* receive side checksum offload */
1015 if (NULL != csum_info) {
1016 /* IP csum offload */
1017 if (csum_info->receive.ip_csum_succeeded) {
1018 m_new->m_pkthdr.csum_flags |=
1019 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1023 /* TCP csum offload */
1024 if (csum_info->receive.tcp_csum_succeeded) {
1025 m_new->m_pkthdr.csum_flags |=
1026 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1027 m_new->m_pkthdr.csum_data = 0xffff;
1031 if (csum_info->receive.ip_csum_succeeded &&
1032 csum_info->receive.tcp_csum_succeeded)
1035 const struct ether_header *eh;
1040 if (m_new->m_len < hoff)
1042 eh = mtod(m_new, struct ether_header *);
1043 etype = ntohs(eh->ether_type);
1044 if (etype == ETHERTYPE_VLAN) {
1045 const struct ether_vlan_header *evl;
1047 hoff = sizeof(*evl);
1048 if (m_new->m_len < hoff)
1050 evl = mtod(m_new, struct ether_vlan_header *);
1051 etype = ntohs(evl->evl_proto);
1054 if (etype == ETHERTYPE_IP) {
1057 pr = hn_check_iplen(m_new, hoff);
1058 if (pr == IPPROTO_TCP) {
1059 if (sc->hn_trust_hosttcp) {
1060 sc->hn_csum_trusted++;
1061 m_new->m_pkthdr.csum_flags |=
1062 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1063 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1064 m_new->m_pkthdr.csum_data = 0xffff;
1066 /* Rely on SW csum verification though... */
1072 if ((packet->vlan_tci != 0) &&
1073 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1074 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1075 m_new->m_flags |= M_VLANTAG;
1079 * Note: Moved RX completion back to hv_nv_on_receive() so all
1080 * messages (not just data messages) will trigger a response.
1083 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1085 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1086 struct lro_ctrl *lro = &sc->hn_lro;
1090 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1097 /* We're not holding the lock here, so don't release it */
1098 (*ifp->if_input)(ifp, m_new);
1104 netvsc_recv_rollup(struct hv_device *device_ctx)
1106 hn_softc_t *sc = device_get_softc(device_ctx->device);
1107 struct lro_ctrl *lro = &sc->hn_lro;
1108 struct lro_entry *queued;
1110 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
1111 SLIST_REMOVE_HEAD(&lro->lro_active, next);
1112 tcp_lro_flush(lro, queued);
1117 * Rules for using sc->temp_unusable:
1118 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1119 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1120 * sc->temp_unusable set, must release NV_LOCK() and exit
1121 * 3. to retain exclusive control of the interface,
1122 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1123 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1124 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1128 * Standard ioctl entry point. Called when the user wants to configure
1132 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1134 hn_softc_t *sc = ifp->if_softc;
1135 struct ifreq *ifr = (struct ifreq *)data;
1137 struct ifaddr *ifa = (struct ifaddr *)data;
1139 netvsc_device_info device_info;
1140 struct hv_device *hn_dev;
1141 int mask, error = 0;
1142 int retry_cnt = 500;
1148 if (ifa->ifa_addr->sa_family == AF_INET) {
1149 ifp->if_flags |= IFF_UP;
1150 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1152 arp_ifinit(ifp, ifa);
1155 error = ether_ioctl(ifp, cmd, data);
1158 hn_dev = vmbus_get_devctx(sc->hn_dev);
1160 /* Check MTU value change */
1161 if (ifp->if_mtu == ifr->ifr_mtu)
1164 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1169 /* Obtain and record requested MTU */
1170 ifp->if_mtu = ifr->ifr_mtu;
1172 * Make sure that LRO high watermark is still valid,
1173 * after MTU change (the 2*MTU limit).
1175 if (!HN_LRO_HIWAT_ISVALID(sc, sc->hn_lro_hiwat))
1176 hn_set_lro_hiwat(sc, HN_LRO_HIWAT_MTULIM(ifp));
1180 if (!sc->temp_unusable) {
1181 sc->temp_unusable = TRUE;
1185 if (retry_cnt > 0) {
1189 } while (retry_cnt > 0);
1191 if (retry_cnt == 0) {
1196 /* We must remove and add back the device to cause the new
1197 * MTU to take effect. This includes tearing down, but not
1198 * deleting the channel, then bringing it back up.
1200 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1203 sc->temp_unusable = FALSE;
1207 error = hv_rf_on_device_add(hn_dev, &device_info);
1210 sc->temp_unusable = FALSE;
1215 hn_ifinit_locked(sc);
1218 sc->temp_unusable = FALSE;
1224 if (!sc->temp_unusable) {
1225 sc->temp_unusable = TRUE;
1229 if (retry_cnt > 0) {
1233 } while (retry_cnt > 0);
1235 if (retry_cnt == 0) {
1240 if (ifp->if_flags & IFF_UP) {
1242 * If only the state of the PROMISC flag changed,
1243 * then just use the 'set promisc mode' command
1244 * instead of reinitializing the entire NIC. Doing
1245 * a full re-init means reloading the firmware and
1246 * waiting for it to start up, which may take a
1250 /* Fixme: Promiscuous mode? */
1251 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1252 ifp->if_flags & IFF_PROMISC &&
1253 !(sc->hn_if_flags & IFF_PROMISC)) {
1254 /* do something here for Hyper-V */
1255 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1256 !(ifp->if_flags & IFF_PROMISC) &&
1257 sc->hn_if_flags & IFF_PROMISC) {
1258 /* do something here for Hyper-V */
1261 hn_ifinit_locked(sc);
1263 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1268 sc->temp_unusable = FALSE;
1270 sc->hn_if_flags = ifp->if_flags;
1274 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1275 if (mask & IFCAP_TXCSUM) {
1276 if (IFCAP_TXCSUM & ifp->if_capenable) {
1277 ifp->if_capenable &= ~IFCAP_TXCSUM;
1278 ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP);
1280 ifp->if_capenable |= IFCAP_TXCSUM;
1282 * Only enable UDP checksum offloading on
1283 * Windows Server 2012R2 or later releases.
1285 if (hv_vmbus_protocal_version >=
1286 HV_VMBUS_VERSION_WIN8_1) {
1288 (CSUM_TCP | CSUM_UDP);
1290 ifp->if_hwassist |= CSUM_TCP;
1295 if (mask & IFCAP_RXCSUM) {
1296 if (IFCAP_RXCSUM & ifp->if_capenable) {
1297 ifp->if_capenable &= ~IFCAP_RXCSUM;
1299 ifp->if_capenable |= IFCAP_RXCSUM;
1302 if (mask & IFCAP_LRO)
1303 ifp->if_capenable ^= IFCAP_LRO;
1305 if (mask & IFCAP_TSO4) {
1306 ifp->if_capenable ^= IFCAP_TSO4;
1307 ifp->if_hwassist ^= CSUM_IP_TSO;
1310 if (mask & IFCAP_TSO6) {
1311 ifp->if_capenable ^= IFCAP_TSO6;
1312 ifp->if_hwassist ^= CSUM_IP6_TSO;
1320 /* Fixme: Multicast mode? */
1321 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1323 netvsc_setmulti(sc);
1332 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1335 error = ether_ioctl(ifp, cmd, data);
1346 hn_stop(hn_softc_t *sc)
1350 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1355 printf(" Closing Device ...\n");
1357 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1358 if_link_state_change(ifp, LINK_STATE_DOWN);
1359 sc->hn_initdone = 0;
1361 ret = hv_rf_on_close(device_ctx);
1365 * FreeBSD transmit entry point
1368 hn_start(struct ifnet *ifp)
1374 if (sc->temp_unusable) {
1378 hn_start_locked(ifp);
1386 hn_ifinit_locked(hn_softc_t *sc)
1389 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1394 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1398 hv_promisc_mode = 1;
1400 ret = hv_rf_on_open(device_ctx);
1404 sc->hn_initdone = 1;
1406 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1407 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1408 if_link_state_change(ifp, LINK_STATE_UP);
1415 hn_ifinit(void *xsc)
1417 hn_softc_t *sc = xsc;
1420 if (sc->temp_unusable) {
1424 sc->temp_unusable = TRUE;
1427 hn_ifinit_locked(sc);
1430 sc->temp_unusable = FALSE;
1439 hn_watchdog(struct ifnet *ifp)
1444 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1445 hn_ifinit(sc); /*???*/
1446 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1452 hn_lro_hiwat_sysctl(SYSCTL_HANDLER_ARGS)
1454 struct hn_softc *sc = arg1;
1457 hiwat = sc->hn_lro_hiwat;
1458 error = sysctl_handle_int(oidp, &hiwat, 0, req);
1459 if (error || req->newptr == NULL)
1462 if (!HN_LRO_HIWAT_ISVALID(sc, hiwat))
1465 if (sc->hn_lro_hiwat != hiwat)
1466 hn_set_lro_hiwat(sc, hiwat);
1469 #endif /* HN_LRO_HIWAT */
1472 hn_check_iplen(const struct mbuf *m, int hoff)
1474 const struct ip *ip;
1475 int len, iphlen, iplen;
1476 const struct tcphdr *th;
1477 int thoff; /* TCP data offset */
1479 len = hoff + sizeof(struct ip);
1481 /* The packet must be at least the size of an IP header. */
1482 if (m->m_pkthdr.len < len)
1483 return IPPROTO_DONE;
1485 /* The fixed IP header must reside completely in the first mbuf. */
1487 return IPPROTO_DONE;
1489 ip = mtodo(m, hoff);
1491 /* Bound check the packet's stated IP header length. */
1492 iphlen = ip->ip_hl << 2;
1493 if (iphlen < sizeof(struct ip)) /* minimum header length */
1494 return IPPROTO_DONE;
1496 /* The full IP header must reside completely in the one mbuf. */
1497 if (m->m_len < hoff + iphlen)
1498 return IPPROTO_DONE;
1500 iplen = ntohs(ip->ip_len);
1503 * Check that the amount of data in the buffers is as
1504 * at least much as the IP header would have us expect.
1506 if (m->m_pkthdr.len < hoff + iplen)
1507 return IPPROTO_DONE;
1510 * Ignore IP fragments.
1512 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
1513 return IPPROTO_DONE;
1516 * The TCP/IP or UDP/IP header must be entirely contained within
1517 * the first fragment of a packet.
1521 if (iplen < iphlen + sizeof(struct tcphdr))
1522 return IPPROTO_DONE;
1523 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
1524 return IPPROTO_DONE;
1525 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
1526 thoff = th->th_off << 2;
1527 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
1528 return IPPROTO_DONE;
1529 if (m->m_len < hoff + iphlen + thoff)
1530 return IPPROTO_DONE;
1533 if (iplen < iphlen + sizeof(struct udphdr))
1534 return IPPROTO_DONE;
1535 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
1536 return IPPROTO_DONE;
1540 return IPPROTO_DONE;
1546 static device_method_t netvsc_methods[] = {
1547 /* Device interface */
1548 DEVMETHOD(device_probe, netvsc_probe),
1549 DEVMETHOD(device_attach, netvsc_attach),
1550 DEVMETHOD(device_detach, netvsc_detach),
1551 DEVMETHOD(device_shutdown, netvsc_shutdown),
1556 static driver_t netvsc_driver = {
1562 static devclass_t netvsc_devclass;
1564 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
1565 MODULE_VERSION(hn, 1);
1566 MODULE_DEPEND(hn, vmbus, 1, 1, 1);