1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Microsoft Corporation
3 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
14 #include <rte_ethdev.h>
15 #include <rte_memcpy.h>
16 #include <rte_string_fns.h>
17 #include <rte_memzone.h>
18 #include <rte_malloc.h>
19 #include <rte_atomic.h>
20 #include <rte_branch_prediction.h>
21 #include <rte_ether.h>
22 #include <rte_common.h>
23 #include <rte_errno.h>
24 #include <rte_memory.h>
28 #include <rte_bus_vmbus.h>
29 #include <rte_spinlock.h>
37 #define HN_NVS_SEND_MSG_SIZE \
38 (sizeof(struct vmbus_chanpkt_hdr) + sizeof(struct hn_nvs_rndis))
40 #define HN_TXD_CACHE_SIZE 32 /* per cpu tx_descriptor pool cache */
41 #define HN_TXCOPY_THRESHOLD 512
43 #define HN_RXCOPY_THRESHOLD 256
44 #define HN_RXQ_EVENT_DEFAULT 2048
53 #define HN_RXINFO_VLAN 0x0001
54 #define HN_RXINFO_CSUM 0x0002
55 #define HN_RXINFO_HASHINF 0x0004
56 #define HN_RXINFO_HASHVAL 0x0008
57 #define HN_RXINFO_ALL \
63 #define HN_NDIS_VLAN_INFO_INVALID 0xffffffff
64 #define HN_NDIS_RXCSUM_INFO_INVALID 0
65 #define HN_NDIS_HASH_INFO_INVALID 0
68 * Per-transmit book keeping.
69 * A slot in transmit ring (chim_index) is reserved for each transmit.
71 * There are two types of transmit:
72 * - buffered transmit where chimney buffer is used and RNDIS header
73 * is in the buffer. mbuf == NULL for this case.
75 * - direct transmit where RNDIS header is in the in rndis_pkt
76 * mbuf is freed after transmit.
78 * Descriptors come from per-port pool which is used
79 * to limit number of outstanding requests per device.
90 struct rndis_packet_msg *rndis_pkt;
93 #define HN_RNDIS_PKT_LEN \
94 (sizeof(struct rndis_packet_msg) + \
95 RNDIS_PKTINFO_SIZE(NDIS_HASH_VALUE_SIZE) + \
96 RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) + \
97 RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) + \
98 RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))
100 /* Minimum space required for a packet */
101 #define HN_PKTSIZE_MIN(align) \
102 RTE_ALIGN(ETHER_MIN_LEN + HN_RNDIS_PKT_LEN, align)
104 #define DEFAULT_TX_FREE_THRESH 32U
107 hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
109 uint32_t s = m->pkt_len;
110 const struct ether_addr *ea;
113 stats->size_bins[1]++;
114 } else if (s > 64 && s < 1024) {
117 /* count zeros, and offset into correct bin */
118 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
119 stats->size_bins[bin]++;
122 stats->size_bins[0]++;
124 stats->size_bins[6]++;
126 stats->size_bins[7]++;
129 ea = rte_pktmbuf_mtod(m, const struct ether_addr *);
130 if (is_multicast_ether_addr(ea)) {
131 if (is_broadcast_ether_addr(ea))
138 static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
140 return pkt->pktinfooffset + pkt->pktinfolen;
143 static inline uint32_t
144 hn_rndis_pktmsg_offset(uint32_t ofs)
146 return ofs - offsetof(struct rndis_packet_msg, dataoffset);
149 static void hn_txd_init(struct rte_mempool *mp __rte_unused,
150 void *opaque, void *obj, unsigned int idx)
152 struct hn_txdesc *txd = obj;
153 struct rte_eth_dev *dev = opaque;
154 struct rndis_packet_msg *pkt;
156 memset(txd, 0, sizeof(*txd));
157 txd->chim_index = idx;
159 pkt = rte_malloc_socket("RNDIS_TX", HN_RNDIS_PKT_LEN,
160 rte_align32pow2(HN_RNDIS_PKT_LEN),
161 dev->device->numa_node);
163 rte_exit(EXIT_FAILURE, "can not allocate RNDIS header");
165 txd->rndis_pkt = pkt;
169 * Unlike Linux and FreeBSD, this driver uses a mempool
170 * to limit outstanding transmits and reserve buffers
173 hn_tx_pool_init(struct rte_eth_dev *dev)
175 struct hn_data *hv = dev->data->dev_private;
176 char name[RTE_MEMPOOL_NAMESIZE];
177 struct rte_mempool *mp;
179 snprintf(name, sizeof(name),
180 "hn_txd_%u", dev->data->port_id);
182 PMD_INIT_LOG(DEBUG, "create a TX send pool %s n=%u size=%zu socket=%d",
183 name, hv->chim_cnt, sizeof(struct hn_txdesc),
184 dev->device->numa_node);
186 mp = rte_mempool_create(name, hv->chim_cnt, sizeof(struct hn_txdesc),
187 HN_TXD_CACHE_SIZE, 0,
190 dev->device->numa_node, 0);
193 "mempool %s create failed: %d", name, rte_errno);
201 static void hn_reset_txagg(struct hn_tx_queue *txq)
203 txq->agg_szleft = txq->agg_szmax;
204 txq->agg_pktleft = txq->agg_pktmax;
206 txq->agg_prevpkt = NULL;
210 hn_dev_tx_queue_setup(struct rte_eth_dev *dev,
211 uint16_t queue_idx, uint16_t nb_desc __rte_unused,
212 unsigned int socket_id,
213 const struct rte_eth_txconf *tx_conf)
216 struct hn_data *hv = dev->data->dev_private;
217 struct hn_tx_queue *txq;
218 uint32_t tx_free_thresh;
220 PMD_INIT_FUNC_TRACE();
222 txq = rte_zmalloc_socket("HN_TXQ", sizeof(*txq), RTE_CACHE_LINE_SIZE,
228 txq->chan = hv->channels[queue_idx];
229 txq->port_id = dev->data->port_id;
230 txq->queue_id = queue_idx;
232 tx_free_thresh = tx_conf->tx_free_thresh;
233 if (tx_free_thresh == 0)
234 tx_free_thresh = RTE_MIN(hv->chim_cnt / 4,
235 DEFAULT_TX_FREE_THRESH);
237 if (tx_free_thresh >= hv->chim_cnt - 3)
238 tx_free_thresh = hv->chim_cnt - 3;
240 txq->free_thresh = tx_free_thresh;
242 txq->agg_szmax = RTE_MIN(hv->chim_szmax, hv->rndis_agg_size);
243 txq->agg_pktmax = hv->rndis_agg_pkts;
244 txq->agg_align = hv->rndis_agg_align;
248 dev->data->tx_queues[queue_idx] = txq;
254 hn_dev_tx_queue_release(void *arg)
256 struct hn_tx_queue *txq = arg;
257 struct hn_txdesc *txd;
259 PMD_INIT_FUNC_TRACE();
264 /* If any pending data is still present just drop it */
267 rte_mempool_put(txq->hv->tx_pool, txd);
273 hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_idx,
274 struct rte_eth_txq_info *qinfo)
276 struct hn_data *hv = dev->data->dev_private;
277 struct hn_tx_queue *txq = dev->data->rx_queues[queue_idx];
279 qinfo->conf.tx_free_thresh = txq->free_thresh;
280 qinfo->nb_desc = hv->tx_pool->size;
284 hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
285 unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
287 struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
288 struct hn_tx_queue *txq;
290 /* Control packets are sent with xacid == 0 */
294 txq = dev->data->tx_queues[queue_id];
295 if (likely(ack->status == NVS_STATUS_OK)) {
296 PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
297 txq->port_id, txq->queue_id, txd->chim_index,
298 txd->packets, txd->data_size);
299 txq->stats.bytes += txd->data_size;
300 txq->stats.packets += txd->packets;
302 PMD_TX_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
303 txq->port_id, txq->queue_id, txd->chim_index, ack->status);
307 rte_pktmbuf_free(txd->m);
309 rte_mempool_put(txq->hv->tx_pool, txd);
312 /* Handle transmit completion events */
314 hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
315 const struct vmbus_chanpkt_hdr *pkt,
318 const struct hn_nvs_hdr *hdr = data;
321 case NVS_TYPE_RNDIS_ACK:
322 hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
327 "unexpected send completion type %u",
332 /* Parse per-packet info (meta data) */
334 hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
335 struct hn_rxinfo *info)
337 const struct rndis_pktinfo *pi = info_data;
340 while (info_dlen != 0) {
344 if (unlikely(info_dlen < sizeof(*pi)))
347 if (unlikely(info_dlen < pi->size))
349 info_dlen -= pi->size;
351 if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
353 if (unlikely(pi->size < pi->offset))
356 dlen = pi->size - pi->offset;
360 case NDIS_PKTINFO_TYPE_VLAN:
361 if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
363 info->vlan_info = *((const uint32_t *)data);
364 mask |= HN_RXINFO_VLAN;
367 case NDIS_PKTINFO_TYPE_CSUM:
368 if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
370 info->csum_info = *((const uint32_t *)data);
371 mask |= HN_RXINFO_CSUM;
374 case NDIS_PKTINFO_TYPE_HASHVAL:
375 if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
377 info->hash_value = *((const uint32_t *)data);
378 mask |= HN_RXINFO_HASHVAL;
381 case NDIS_PKTINFO_TYPE_HASHINF:
382 if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
384 info->hash_info = *((const uint32_t *)data);
385 mask |= HN_RXINFO_HASHINF;
392 if (mask == HN_RXINFO_ALL)
393 break; /* All found; done */
395 pi = (const struct rndis_pktinfo *)
396 ((const uint8_t *)pi + pi->size);
401 * - If there is no hash value, invalidate the hash info.
403 if (!(mask & HN_RXINFO_HASHVAL))
404 info->hash_info = HN_NDIS_HASH_INFO_INVALID;
409 * Ack the consumed RXBUF associated w/ this channel packet,
410 * so that this RXBUF can be recycled by the hypervisor.
412 static void hn_rx_buf_release(struct hn_rx_bufinfo *rxb)
414 struct rte_mbuf_ext_shared_info *shinfo = &rxb->shinfo;
415 struct hn_data *hv = rxb->hv;
417 if (rte_mbuf_ext_refcnt_update(shinfo, -1) == 0) {
418 hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
419 --hv->rxbuf_outstanding;
423 static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
425 hn_rx_buf_release(opaque);
428 static struct hn_rx_bufinfo *hn_rx_buf_init(const struct hn_rx_queue *rxq,
429 const struct vmbus_chanpkt_rxbuf *pkt)
431 struct hn_rx_bufinfo *rxb;
433 rxb = rxq->hv->rxbuf_info + pkt->hdr.xactid;
434 rxb->chan = rxq->chan;
435 rxb->xactid = pkt->hdr.xactid;
438 rxb->shinfo.free_cb = hn_rx_buf_free_cb;
439 rxb->shinfo.fcb_opaque = rxb;
440 rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
444 static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
445 uint8_t *data, unsigned int headroom, unsigned int dlen,
446 const struct hn_rxinfo *info)
448 struct hn_data *hv = rxq->hv;
451 m = rte_pktmbuf_alloc(rxq->mb_pool);
453 struct rte_eth_dev *dev =
454 &rte_eth_devices[rxq->port_id];
456 dev->data->rx_mbuf_alloc_failed++;
461 * For large packets, avoid copy if possible but need to keep
462 * some space available in receive area for later packets.
464 if (dlen >= HN_RXCOPY_THRESHOLD &&
465 hv->rxbuf_outstanding < hv->rxbuf_section_cnt / 2) {
466 struct rte_mbuf_ext_shared_info *shinfo;
471 * Build an external mbuf that points to recveive area.
472 * Use refcount to handle multiple packets in same
473 * receive buffer section.
475 rxbuf = hv->rxbuf_res->addr;
476 iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
477 shinfo = &rxb->shinfo;
479 if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 1)
480 ++hv->rxbuf_outstanding;
482 rte_pktmbuf_attach_extbuf(m, data, iova,
483 dlen + headroom, shinfo);
484 m->data_off = headroom;
486 /* Mbuf's in pool must be large enough to hold small packets */
487 if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
488 rte_pktmbuf_free_seg(m);
492 rte_memcpy(rte_pktmbuf_mtod(m, void *),
493 data + headroom, dlen);
496 m->port = rxq->port_id;
499 m->packet_type = rte_net_get_ptype(m, NULL,
504 if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
505 m->vlan_tci = info->vlan_info;
506 m->ol_flags |= PKT_RX_VLAN_STRIPPED | PKT_RX_VLAN;
509 if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
510 if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
511 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
513 if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
514 | NDIS_RXCSUM_INFO_TCPCS_OK))
515 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
518 if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
519 m->ol_flags |= PKT_RX_RSS_HASH;
520 m->hash.rss = info->hash_value;
524 "port %u:%u RX id %"PRIu64" size %u type %#x ol_flags %#"PRIx64,
525 rxq->port_id, rxq->queue_id, rxb->xactid,
526 m->pkt_len, m->packet_type, m->ol_flags);
528 ++rxq->stats.packets;
529 rxq->stats.bytes += m->pkt_len;
530 hn_update_packet_stats(&rxq->stats, m);
532 if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
538 static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
539 struct hn_rx_bufinfo *rxb,
540 void *data, uint32_t dlen)
542 unsigned int data_off, data_len, pktinfo_off, pktinfo_len;
543 const struct rndis_packet_msg *pkt = data;
544 struct hn_rxinfo info = {
545 .vlan_info = HN_NDIS_VLAN_INFO_INVALID,
546 .csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
547 .hash_info = HN_NDIS_HASH_INFO_INVALID,
553 if (unlikely(dlen < sizeof(*pkt)))
556 if (unlikely(dlen < pkt->len))
557 goto error; /* truncated RNDIS from host */
559 if (unlikely(pkt->len < pkt->datalen
560 + pkt->oobdatalen + pkt->pktinfolen))
563 if (unlikely(pkt->datalen == 0))
567 if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
570 if (likely(pkt->pktinfooffset > 0) &&
571 unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
572 (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
575 data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
576 data_len = pkt->datalen;
577 pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
578 pktinfo_len = pkt->pktinfolen;
580 if (likely(pktinfo_len > 0)) {
581 err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
587 if (unlikely(data_off + data_len > pkt->len))
590 if (unlikely(data_len < ETHER_HDR_LEN))
593 hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
600 hn_rndis_receive(const struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
601 struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
603 const struct rndis_msghdr *hdr = buf;
606 case RNDIS_PACKET_MSG:
607 if (dev->data->dev_started)
608 hn_rndis_rx_data(rxq, rxb, buf, len);
611 case RNDIS_INDICATE_STATUS_MSG:
612 hn_rndis_link_status(rxq->hv, buf);
615 case RNDIS_INITIALIZE_CMPLT:
616 case RNDIS_QUERY_CMPLT:
617 case RNDIS_SET_CMPLT:
618 hn_rndis_receive_response(rxq->hv, buf, len);
623 "unexpected RNDIS message (type %#x len %u)",
630 hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
632 struct hn_rx_queue *rxq,
633 const struct vmbus_chanpkt_hdr *hdr,
636 const struct vmbus_chanpkt_rxbuf *pkt;
637 const struct hn_nvs_hdr *nvs_hdr = buf;
638 uint32_t rxbuf_sz = hv->rxbuf_res->len;
639 char *rxbuf = hv->rxbuf_res->addr;
640 unsigned int i, hlen, count;
641 struct hn_rx_bufinfo *rxb;
643 /* At minimum we need type header */
644 if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
645 PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
649 /* Make sure that this is a RNDIS message. */
650 if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
651 PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
656 hlen = vmbus_chanpkt_getlen(hdr->hlen);
657 if (unlikely(hlen < sizeof(*pkt))) {
658 PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
662 pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
663 if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
664 PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
669 count = pkt->rxbuf_cnt;
670 if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
672 PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
676 if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
677 PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
682 /* Setup receive buffer info to allow for callback */
683 rxb = hn_rx_buf_init(rxq, pkt);
685 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
686 for (i = 0; i < count; ++i) {
687 unsigned int ofs, len;
689 ofs = pkt->rxbuf[i].ofs;
690 len = pkt->rxbuf[i].len;
692 if (unlikely(ofs + len > rxbuf_sz)) {
694 "%uth RNDIS msg overflow ofs %u, len %u",
699 if (unlikely(len == 0)) {
700 PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
704 hn_rndis_receive(dev, rxq, rxb,
708 /* Send ACK now if external mbuf not used */
709 hn_rx_buf_release(rxb);
712 struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
714 unsigned int socket_id)
716 struct hn_rx_queue *rxq;
718 rxq = rte_zmalloc_socket("HN_RXQ",
719 sizeof(*rxq) + HN_RXQ_EVENT_DEFAULT,
720 RTE_CACHE_LINE_SIZE, socket_id);
723 rxq->chan = hv->channels[queue_id];
724 rte_spinlock_init(&rxq->ring_lock);
725 rxq->port_id = hv->port_id;
726 rxq->queue_id = queue_id;
732 hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
733 uint16_t queue_idx, uint16_t nb_desc,
734 unsigned int socket_id,
735 const struct rte_eth_rxconf *rx_conf __rte_unused,
736 struct rte_mempool *mp)
738 struct hn_data *hv = dev->data->dev_private;
739 char ring_name[RTE_RING_NAMESIZE];
740 struct hn_rx_queue *rxq;
743 PMD_INIT_FUNC_TRACE();
745 if (queue_idx == 0) {
748 rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
754 count = rte_mempool_avail_count(mp) / dev->data->nb_rx_queues;
755 if (nb_desc == 0 || nb_desc > count)
759 * Staging ring from receive event logic to rx_pkts.
760 * rx_pkts assumes caller is handling multi-thread issue.
761 * event logic has locking.
763 snprintf(ring_name, sizeof(ring_name),
764 "hn_rx_%u_%u", dev->data->port_id, queue_idx);
765 rxq->rx_ring = rte_ring_create(ring_name,
766 rte_align32pow2(nb_desc),
771 dev->data->rx_queues[queue_idx] = rxq;
775 rte_ring_free(rxq->rx_ring);
776 rte_free(rxq->event_buf);
782 hn_dev_rx_queue_release(void *arg)
784 struct hn_rx_queue *rxq = arg;
786 PMD_INIT_FUNC_TRACE();
791 rte_ring_free(rxq->rx_ring);
795 if (rxq != rxq->hv->primary) {
796 rte_free(rxq->event_buf);
802 hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_idx,
803 struct rte_eth_rxq_info *qinfo)
805 struct hn_rx_queue *rxq = dev->data->rx_queues[queue_idx];
807 qinfo->mp = rxq->mb_pool;
808 qinfo->scattered_rx = 1;
809 qinfo->nb_desc = rte_ring_get_capacity(rxq->rx_ring);
813 hn_nvs_handle_notify(const struct vmbus_chanpkt_hdr *pkthdr,
816 const struct hn_nvs_hdr *hdr = data;
818 if (unlikely(vmbus_chanpkt_datalen(pkthdr) < sizeof(*hdr))) {
819 PMD_DRV_LOG(ERR, "invalid nvs notify");
824 "got notify, nvs type %u", hdr->type);
828 * Process pending events on the channel.
829 * Called from both Rx queue poll and Tx cleanup
831 void hn_process_events(struct hn_data *hv, uint16_t queue_id)
833 struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
834 struct hn_rx_queue *rxq;
835 uint32_t bytes_read = 0;
838 rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];
840 /* If no pending data then nothing to do */
841 if (rte_vmbus_chan_rx_empty(rxq->chan))
845 * Since channel is shared between Rx and TX queue need to have a lock
846 * since DPDK does not force same CPU to be used for Rx/Tx.
848 if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
852 const struct vmbus_chanpkt_hdr *pkt;
853 uint32_t len = HN_RXQ_EVENT_DEFAULT;
856 ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
858 break; /* ring is empty */
860 else if (ret == -ENOBUFS)
861 rte_exit(EXIT_FAILURE, "event buffer not big enough (%u < %u)",
862 HN_RXQ_EVENT_DEFAULT, len);
864 rte_exit(EXIT_FAILURE,
865 "vmbus ring buffer error: %d", ret);
868 pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
869 data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);
872 case VMBUS_CHANPKT_TYPE_COMP:
873 hn_nvs_handle_comp(dev, queue_id, pkt, data);
876 case VMBUS_CHANPKT_TYPE_RXBUF:
877 hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
880 case VMBUS_CHANPKT_TYPE_INBAND:
881 hn_nvs_handle_notify(pkt, data);
885 PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
889 if (rxq->rx_ring && rte_ring_full(rxq->rx_ring))
894 rte_vmbus_chan_signal_read(rxq->chan, bytes_read);
896 rte_spinlock_unlock(&rxq->ring_lock);
899 static void hn_append_to_chim(struct hn_tx_queue *txq,
900 struct rndis_packet_msg *pkt,
901 const struct rte_mbuf *m)
903 struct hn_txdesc *txd = txq->agg_txd;
904 uint8_t *buf = (uint8_t *)pkt;
905 unsigned int data_offs;
909 data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
910 txd->chim_size += pkt->len;
911 txd->data_size += m->pkt_len;
913 hn_update_packet_stats(&txq->stats, m);
915 for (; m; m = m->next) {
916 uint16_t len = rte_pktmbuf_data_len(m);
918 rte_memcpy(buf + data_offs,
919 rte_pktmbuf_mtod(m, const char *), len);
925 * Send pending aggregated data in chimney buffer (if any).
926 * Returns error if send was unsuccessful because channel ring buffer
929 static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)
932 struct hn_txdesc *txd = txq->agg_txd;
933 struct hn_nvs_rndis rndis;
939 rndis = (struct hn_nvs_rndis) {
940 .type = NVS_TYPE_RNDIS,
941 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
942 .chim_idx = txd->chim_index,
943 .chim_sz = txd->chim_size,
946 PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
947 txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);
949 ret = hn_nvs_send(txq->chan, VMBUS_CHANPKT_FLAG_RC,
950 &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);
952 if (likely(ret == 0))
955 PMD_TX_LOG(NOTICE, "port %u:%u send failed: %d",
956 txq->port_id, txq->queue_id, ret);
961 static struct hn_txdesc *hn_new_txd(struct hn_data *hv,
962 struct hn_tx_queue *txq)
964 struct hn_txdesc *txd;
966 if (rte_mempool_get(hv->tx_pool, (void **)&txd)) {
967 ++txq->stats.nomemory;
968 PMD_TX_LOG(DEBUG, "tx pool exhausted!");
973 txd->queue_id = txq->queue_id;
982 hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq, uint32_t pktsize)
984 struct hn_txdesc *agg_txd = txq->agg_txd;
985 struct rndis_packet_msg *pkt;
989 unsigned int padding, olen;
992 * Update the previous RNDIS packet's total length,
993 * it can be increased due to the mandatory alignment
994 * padding for this RNDIS packet. And update the
995 * aggregating txdesc's chimney sending buffer size
998 * Zero-out the padding, as required by the RNDIS spec.
1000 pkt = txq->agg_prevpkt;
1002 padding = RTE_ALIGN(olen, txq->agg_align) - olen;
1004 agg_txd->chim_size += padding;
1005 pkt->len += padding;
1006 memset((uint8_t *)pkt + olen, 0, padding);
1009 chim = (uint8_t *)pkt + pkt->len;
1012 txq->agg_szleft -= pktsize;
1013 if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
1015 * Probably can't aggregate more packets,
1016 * flush this aggregating txdesc proactively.
1018 txq->agg_pktleft = 0;
1021 agg_txd = hn_new_txd(hv, txq);
1025 chim = (uint8_t *)hv->chim_res->addr
1026 + agg_txd->chim_index * hv->chim_szmax;
1028 txq->agg_txd = agg_txd;
1029 txq->agg_pktleft = txq->agg_pktmax - 1;
1030 txq->agg_szleft = txq->agg_szmax - pktsize;
1032 txq->agg_prevpkt = chim;
1037 static inline void *
1038 hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
1039 uint32_t pi_dlen, uint32_t pi_type)
1041 const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
1042 struct rndis_pktinfo *pi;
1045 * Per-packet-info does not move; it only grows.
1048 * pktinfooffset in this phase counts from the beginning
1049 * of rndis_packet_msg.
1051 pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));
1053 pkt->pktinfolen += pi_size;
1057 pi->offset = RNDIS_PKTINFO_OFFSET;
1062 /* Put RNDIS header and packet info on packet */
1063 static void hn_encap(struct rndis_packet_msg *pkt,
1065 const struct rte_mbuf *m)
1067 unsigned int hlen = m->l2_len + m->l3_len;
1071 pkt->type = RNDIS_PACKET_MSG;
1072 pkt->len = m->pkt_len;
1073 pkt->dataoffset = 0;
1074 pkt->datalen = m->pkt_len;
1075 pkt->oobdataoffset = 0;
1076 pkt->oobdatalen = 0;
1077 pkt->oobdataelements = 0;
1078 pkt->pktinfooffset = sizeof(*pkt);
1079 pkt->pktinfolen = 0;
1084 * Set the hash value for this packet, to the queue_id to cause
1085 * TX done event for this packet on the right channel.
1087 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
1088 NDIS_PKTINFO_TYPE_HASHVAL);
1089 *pi_data = queue_id;
1091 if (m->ol_flags & PKT_TX_VLAN_PKT) {
1092 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
1093 NDIS_PKTINFO_TYPE_VLAN);
1094 *pi_data = m->vlan_tci;
1097 if (m->ol_flags & PKT_TX_TCP_SEG) {
1098 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
1099 NDIS_PKTINFO_TYPE_LSO);
1101 if (m->ol_flags & PKT_TX_IPV6) {
1102 *pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
1105 *pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
1108 } else if (m->ol_flags &
1109 (PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM)) {
1110 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
1111 NDIS_PKTINFO_TYPE_CSUM);
1114 if (m->ol_flags & PKT_TX_IPV6)
1115 *pi_data |= NDIS_TXCSUM_INFO_IPV6;
1116 if (m->ol_flags & PKT_TX_IPV4) {
1117 *pi_data |= NDIS_TXCSUM_INFO_IPV4;
1119 if (m->ol_flags & PKT_TX_IP_CKSUM)
1120 *pi_data |= NDIS_TXCSUM_INFO_IPCS;
1123 if (m->ol_flags & PKT_TX_TCP_CKSUM)
1124 *pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
1125 else if (m->ol_flags & PKT_TX_UDP_CKSUM)
1126 *pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
1129 pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
1130 /* Fixup RNDIS packet message total length */
1131 pkt->len += pkt_hlen;
1133 /* Convert RNDIS packet message offsets */
1134 pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
1135 pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
1138 /* How many scatter gather list elements ar needed */
1139 static unsigned int hn_get_slots(const struct rte_mbuf *m)
1141 unsigned int slots = 1; /* for RNDIS header */
1144 unsigned int size = rte_pktmbuf_data_len(m);
1145 unsigned int offs = rte_mbuf_data_iova(m) & PAGE_MASK;
1147 slots += (offs + size + PAGE_SIZE - 1) / PAGE_SIZE;
1154 /* Build scatter gather list from chained mbuf */
1155 static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
1156 const struct rte_mbuf *m)
1158 unsigned int segs = 0;
1161 rte_iova_t addr = rte_mbuf_data_iova(m);
1162 unsigned int page = addr / PAGE_SIZE;
1163 unsigned int offset = addr & PAGE_MASK;
1164 unsigned int len = rte_pktmbuf_data_len(m);
1167 unsigned int bytes = RTE_MIN(len, PAGE_SIZE - offset);
1169 sg[segs].page = page;
1170 sg[segs].ofs = offset;
1171 sg[segs].len = bytes;
1184 /* Transmit directly from mbuf */
1185 static int hn_xmit_sg(struct hn_tx_queue *txq,
1186 const struct hn_txdesc *txd, const struct rte_mbuf *m,
1189 struct vmbus_gpa sg[hn_get_slots(m)];
1190 struct hn_nvs_rndis nvs_rndis = {
1191 .type = NVS_TYPE_RNDIS,
1192 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1193 .chim_sz = txd->chim_size,
1198 /* attach aggregation data if present */
1199 if (txd->chim_size > 0)
1200 nvs_rndis.chim_idx = txd->chim_index;
1202 nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;
1204 hn_rndis_dump(txd->rndis_pkt);
1206 /* pass IOVA of rndis header in first segment */
1207 addr = rte_malloc_virt2iova(txd->rndis_pkt);
1208 if (unlikely(addr == RTE_BAD_IOVA)) {
1209 PMD_DRV_LOG(ERR, "RNDIS transmit can not get iova");
1213 sg[0].page = addr / PAGE_SIZE;
1214 sg[0].ofs = addr & PAGE_MASK;
1215 sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
1218 hn_update_packet_stats(&txq->stats, m);
1220 segs += hn_fill_sg(sg + 1, m);
1222 PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
1223 txq->port_id, txq->queue_id, txd->chim_index,
1224 segs, nvs_rndis.chim_sz);
1226 return hn_nvs_send_sglist(txq->chan, sg, segs,
1227 &nvs_rndis, sizeof(nvs_rndis),
1228 (uintptr_t)txd, need_sig);
1232 hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1234 struct hn_tx_queue *txq = ptxq;
1235 struct hn_data *hv = txq->hv;
1236 bool need_sig = false;
1240 if (unlikely(hv->closed))
1243 if (rte_mempool_avail_count(hv->tx_pool) <= txq->free_thresh)
1244 hn_process_events(hv, txq->queue_id);
1246 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1247 struct rte_mbuf *m = tx_pkts[nb_tx];
1248 uint32_t pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
1249 struct rndis_packet_msg *pkt;
1251 /* For small packets aggregate them in chimney buffer */
1252 if (m->pkt_len < HN_TXCOPY_THRESHOLD && pkt_size <= txq->agg_szmax) {
1253 /* If this packet will not fit, then flush */
1254 if (txq->agg_pktleft == 0 ||
1255 RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
1256 if (hn_flush_txagg(txq, &need_sig))
1260 pkt = hn_try_txagg(hv, txq, pkt_size);
1264 hn_encap(pkt, txq->queue_id, m);
1265 hn_append_to_chim(txq, pkt, m);
1267 rte_pktmbuf_free(m);
1269 /* if buffer is full, flush */
1270 if (txq->agg_pktleft == 0 &&
1271 hn_flush_txagg(txq, &need_sig))
1274 struct hn_txdesc *txd;
1276 /* can send chimney data and large packet at once */
1279 hn_reset_txagg(txq);
1281 txd = hn_new_txd(hv, txq);
1286 pkt = txd->rndis_pkt;
1288 txd->data_size += m->pkt_len;
1291 hn_encap(pkt, txq->queue_id, m);
1293 ret = hn_xmit_sg(txq, txd, m, &need_sig);
1294 if (unlikely(ret != 0)) {
1295 PMD_TX_LOG(NOTICE, "sg send failed: %d", ret);
1296 ++txq->stats.errors;
1297 rte_mempool_put(hv->tx_pool, txd);
1303 /* If partial buffer left, then try and send it.
1304 * if that fails, then reuse it on next send.
1306 hn_flush_txagg(txq, &need_sig);
1310 rte_vmbus_chan_signal_tx(txq->chan);
1316 hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1318 struct hn_rx_queue *rxq = prxq;
1319 struct hn_data *hv = rxq->hv;
1321 if (unlikely(hv->closed))
1324 /* If ring is empty then process more */
1325 if (rte_ring_count(rxq->rx_ring) < nb_pkts)
1326 hn_process_events(hv, rxq->queue_id);
1328 /* Get mbufs off staging ring */
1329 return rte_ring_sc_dequeue_burst(rxq->rx_ring, (void **)rx_pkts,