+/* if possible, calculate the checksum of a packet in hw or sw,
+ * depending on the testpmd command line configuration */
+static uint64_t
+process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
+ uint64_t tx_offloads)
+{
+ struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
+ struct rte_udp_hdr *udp_hdr;
+ struct rte_tcp_hdr *tcp_hdr;
+ struct rte_sctp_hdr *sctp_hdr;
+ uint64_t ol_flags = 0;
+ uint32_t max_pkt_len, tso_segsz = 0;
+
+ /* ensure packet is large enough to require tso */
+ if (!info->is_tunnel) {
+ max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
+ info->tso_segsz;
+ if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
+ tso_segsz = info->tso_segsz;
+ } else {
+ max_pkt_len = info->outer_l2_len + info->outer_l3_len +
+ info->l2_len + info->l3_len + info->l4_len +
+ info->tunnel_tso_segsz;
+ if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
+ tso_segsz = info->tunnel_tso_segsz;
+ }
+
+ if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
+ ipv4_hdr = l3_hdr;
+
+ ol_flags |= RTE_MBUF_F_TX_IPV4;
+ if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
+ ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
+ } else {
+ if (tx_offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM) {
+ ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
+ } else {
+ ipv4_hdr->hdr_checksum = 0;
+ ipv4_hdr->hdr_checksum =
+ rte_ipv4_cksum(ipv4_hdr);
+ }
+ }
+ } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
+ ol_flags |= RTE_MBUF_F_TX_IPV6;
+ else
+ return 0; /* packet type not supported, nothing to do */
+
+ if (info->l4_proto == IPPROTO_UDP) {
+ udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
+ /* do not recalculate udp cksum if it was 0 */
+ if (udp_hdr->dgram_cksum != 0) {
+ if (tx_offloads & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
+ ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
+ } else {
+ udp_hdr->dgram_cksum = 0;
+ udp_hdr->dgram_cksum =
+ get_udptcp_checksum(l3_hdr, udp_hdr,
+ info->ethertype);
+ }
+ }
+#ifdef RTE_LIB_GSO
+ if (info->gso_enable)
+ ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
+#endif
+ } else if (info->l4_proto == IPPROTO_TCP) {
+ tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
+ if (tso_segsz)
+ ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
+ else if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
+ ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
+ } else {
+ tcp_hdr->cksum = 0;
+ tcp_hdr->cksum =
+ get_udptcp_checksum(l3_hdr, tcp_hdr,
+ info->ethertype);
+ }
+#ifdef RTE_LIB_GSO
+ if (info->gso_enable)
+ ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
+#endif
+ } else if (info->l4_proto == IPPROTO_SCTP) {
+ sctp_hdr = (struct rte_sctp_hdr *)
+ ((char *)l3_hdr + info->l3_len);
+ /* sctp payload must be a multiple of 4 to be
+ * offloaded */
+ if ((tx_offloads & RTE_ETH_TX_OFFLOAD_SCTP_CKSUM) &&
+ ((ipv4_hdr->total_length & 0x3) == 0)) {
+ ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
+ } else {
+ sctp_hdr->cksum = 0;
+ /* XXX implement CRC32c, example available in
+ * RFC3309 */
+ }
+ }
+
+ return ol_flags;
+}
+
+/* Calculate the checksum of outer header */
+static uint64_t
+process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
+ uint64_t tx_offloads, int tso_enabled)
+{
+ struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
+ struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
+ struct rte_udp_hdr *udp_hdr;
+ uint64_t ol_flags = 0;
+
+ if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
+ ipv4_hdr->hdr_checksum = 0;
+ ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
+
+ if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM)
+ ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
+ else
+ ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
+ } else
+ ol_flags |= RTE_MBUF_F_TX_OUTER_IPV6;
+
+ if (info->outer_l4_proto != IPPROTO_UDP)
+ return ol_flags;
+
+ udp_hdr = (struct rte_udp_hdr *)
+ ((char *)outer_l3_hdr + info->outer_l3_len);
+
+ if (tso_enabled)
+ ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
+
+ /* Skip SW outer UDP checksum generation if HW supports it */
+ if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) {
+ if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
+ udp_hdr->dgram_cksum
+ = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
+ else
+ udp_hdr->dgram_cksum
+ = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
+
+ ol_flags |= RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
+ return ol_flags;
+ }
+
+ /* outer UDP checksum is done in software. In the other side, for
+ * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
+ * set to zero.
+ *
+ * If a packet will be TSOed into small packets by NIC, we cannot
+ * set/calculate a non-zero checksum, because it will be a wrong
+ * value after the packet be split into several small packets.
+ */
+ if (tso_enabled)
+ udp_hdr->dgram_cksum = 0;
+
+ /* do not recalculate udp cksum if it was 0 */
+ if (udp_hdr->dgram_cksum != 0) {
+ udp_hdr->dgram_cksum = 0;
+ if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
+ udp_hdr->dgram_cksum =
+ rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
+ else
+ udp_hdr->dgram_cksum =
+ rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
+ }
+
+ return ol_flags;
+}
+
+/*
+ * Helper function.
+ * Performs actual copying.
+ * Returns number of segments in the destination mbuf on success,
+ * or negative error code on failure.
+ */
+static int
+mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
+ uint16_t seglen[], uint8_t nb_seg)
+{
+ uint32_t dlen, slen, tlen;
+ uint32_t i, len;
+ const struct rte_mbuf *m;
+ const uint8_t *src;
+ uint8_t *dst;
+
+ dlen = 0;
+ slen = 0;
+ tlen = 0;
+
+ dst = NULL;
+ src = NULL;
+
+ m = ms;
+ i = 0;
+ while (ms != NULL && i != nb_seg) {
+
+ if (slen == 0) {
+ slen = rte_pktmbuf_data_len(ms);
+ src = rte_pktmbuf_mtod(ms, const uint8_t *);
+ }
+
+ if (dlen == 0) {
+ dlen = RTE_MIN(seglen[i], slen);
+ md[i]->data_len = dlen;
+ md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
+ dst = rte_pktmbuf_mtod(md[i], uint8_t *);
+ }
+
+ len = RTE_MIN(slen, dlen);
+ memcpy(dst, src, len);
+ tlen += len;
+ slen -= len;
+ dlen -= len;
+ src += len;
+ dst += len;
+
+ if (slen == 0)
+ ms = ms->next;
+ if (dlen == 0)
+ i++;
+ }
+
+ if (ms != NULL)
+ return -ENOBUFS;
+ else if (tlen != m->pkt_len)
+ return -EINVAL;
+
+ md[0]->nb_segs = nb_seg;
+ md[0]->pkt_len = tlen;
+ md[0]->vlan_tci = m->vlan_tci;
+ md[0]->vlan_tci_outer = m->vlan_tci_outer;
+ md[0]->ol_flags = m->ol_flags;
+ md[0]->tx_offload = m->tx_offload;
+
+ return nb_seg;
+}