Hi Olivier,
I noticed that __rte_pktmbuf_read() could do with an unlikely(), so I went through the entire library. Here are my suggested modifications. diff -bu rte_mbuf.c.orig rte_mbuf.c --- rte_mbuf.c.orig 2018-07-23 15:13:22.000000000 +0200 +++ rte_mbuf.c 2018-07-23 15:32:53.000000000 +0200 @@ -173,19 +173,19 @@ { unsigned int nb_segs, pkt_len; - if (m == NULL) + if (unlikely(m == NULL)) rte_panic("mbuf is NULL\n"); /* generic checks */ - if (m->pool == NULL) + if (unlikely(m->pool == NULL)) rte_panic("bad mbuf pool\n"); - if (m->buf_iova == 0) + if (unlikely(m->buf_iova == 0)) rte_panic("bad IO addr\n"); - if (m->buf_addr == NULL) + if (unlikely(m->buf_addr == NULL)) rte_panic("bad virt addr\n"); uint16_t cnt = rte_mbuf_refcnt_read(m); - if ((cnt == 0) || (cnt == UINT16_MAX)) + if (unlikely((cnt == 0) || (cnt == UINT16_MAX))) rte_panic("bad ref cnt\n"); /* nothing to check for sub-segments */ @@ -193,7 +193,7 @@ return; /* data_len is supposed to be not more than pkt_len */ - if (m->data_len > m->pkt_len) + if (unlikely(m->data_len > m->pkt_len)) rte_panic("bad data_len\n"); nb_segs = m->nb_segs; @@ -204,9 +204,9 @@ pkt_len -= m->data_len; } while ((m = m->next) != NULL); - if (nb_segs) + if (unlikely(nb_segs)) rte_panic("bad nb_segs\n"); - if (pkt_len) + if (unlikely(pkt_len)) rte_panic("bad pkt_len\n"); } @@ -249,7 +249,7 @@ const struct rte_mbuf *seg = m; uint32_t buf_off = 0, copy_len; - if (off + len > rte_pktmbuf_pkt_len(m)) + if (unlikely(off + len > rte_pktmbuf_pkt_len(m))) return NULL; while (off >= rte_pktmbuf_data_len(seg)) { @@ -257,7 +257,7 @@ seg = seg->next; } - if (off + len <= rte_pktmbuf_data_len(seg)) + if (likely(off + len <= rte_pktmbuf_data_len(seg))) return rte_pktmbuf_mtod_offset(seg, char *, off); /* rare case: header is split among several segments */ @@ -344,7 +344,7 @@ unsigned int i; int ret; - if (buflen == 0) + if (unlikely(buflen == 0)) return -1; buf[0] = '\0'; @@ -355,9 +355,9 @@ if (name == NULL) name = rx_flags[i].default_name; ret = snprintf(buf, buflen, "%s ", name); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -440,7 +440,7 @@ unsigned int i; int ret; - if (buflen == 0) + if (unlikely(buflen == 0)) return -1; buf[0] = '\0'; @@ -451,9 +451,9 @@ if (name == NULL) name = tx_flags[i].default_name; ret = snprintf(buf, buflen, "%s ", name); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; diff -bu rte_mbuf.h.orig rte_mbuf.h --- rte_mbuf.h.orig 2018-07-23 15:13:26.000000000 +0200 +++ rte_mbuf.h 2018-07-23 15:24:25.000000000 +0200 @@ -1007,7 +1007,7 @@ { struct rte_mbuf *m; - if (rte_mempool_get(mp, (void **)&m) < 0) + if (unlikely(rte_mempool_get(mp, (void **)&m) < 0)) return NULL; MBUF_RAW_ALLOC_CHECK(m); return m; @@ -1268,7 +1268,7 @@ static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp) { struct rte_mbuf *m; - if ((m = rte_mbuf_raw_alloc(mp)) != NULL) + if (likely((m = rte_mbuf_raw_alloc(mp)) != NULL)) rte_pktmbuf_reset(m); return m; } @@ -1696,7 +1696,7 @@ { struct rte_mbuf *m_next; - if (m != NULL) + if (likely(m != NULL)) __rte_mbuf_sanity_check(m, 1); while (m != NULL) { @@ -2099,7 +2099,7 @@ struct rte_mbuf *cur_tail; /* Check for number-of-segments-overflow */ - if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS) + if (unlikely(head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)) return -EOVERFLOW; /* Chain 'tail' onto the old tail */ @@ -2147,28 +2147,28 @@ m->outer_l3_len; /* Headers are fragmented */ - if (rte_pktmbuf_data_len(m) < inner_l3_offset + m->l3_len + m->l4_len) + if (unlikely(rte_pktmbuf_data_len(m) < inner_l3_offset + m->l3_len + m->l4_len)) return -ENOTSUP; /* IP checksum can be counted only for IPv4 packet */ - if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6)) + if (unlikely((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))) return -EINVAL; /* IP type not set when required */ if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG)) - if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6))) + if (unlikely(!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))) return -EINVAL; /* Check requirements for TSO packet */ if (ol_flags & PKT_TX_TCP_SEG) - if ((m->tso_segsz == 0) || + if (unlikely((m->tso_segsz == 0) || ((ol_flags & PKT_TX_IPV4) && - !(ol_flags & PKT_TX_IP_CKSUM))) + !(ol_flags & PKT_TX_IP_CKSUM)))) return -EINVAL; /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */ - if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) && - !(ol_flags & PKT_TX_OUTER_IPV4)) + if (unlikely((ol_flags & PKT_TX_OUTER_IP_CKSUM) && + !(ol_flags & PKT_TX_OUTER_IPV4))) return -EINVAL; return 0; diff -bu rte_mbuf_ptype.c.orig rte_mbuf_ptype.c --- rte_mbuf_ptype.c.orig 2018-07-23 15:45:49.000000000 +0200 +++ rte_mbuf_ptype.c 2018-07-23 15:44:59.000000000 +0200 @@ -118,15 +118,15 @@ { int ret; - if (buflen == 0) + if (unlikely(buflen == 0)) return -1; buf[0] = '\0'; if ((ptype & RTE_PTYPE_ALL_MASK) == RTE_PTYPE_UNKNOWN) { ret = snprintf(buf, buflen, "UNKNOWN"); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; return 0; } @@ -134,9 +134,9 @@ if ((ptype & RTE_PTYPE_L2_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_l2_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -144,9 +144,9 @@ if ((ptype & RTE_PTYPE_L3_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_l3_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -154,9 +154,9 @@ if ((ptype & RTE_PTYPE_L4_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_l4_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -164,9 +164,9 @@ if ((ptype & RTE_PTYPE_TUNNEL_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_tunnel_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -174,9 +174,9 @@ if ((ptype & RTE_PTYPE_INNER_L2_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_inner_l2_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -184,9 +184,9 @@ if ((ptype & RTE_PTYPE_INNER_L3_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_inner_l3_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; @@ -194,9 +194,9 @@ if ((ptype & RTE_PTYPE_INNER_L4_MASK) != 0) { ret = snprintf(buf, buflen, "%s ", rte_get_ptype_inner_l4_name(ptype)); - if (ret < 0) + if (unlikely(ret < 0)) return -1; - if ((size_t)ret >= buflen) + if (unlikely((size_t)ret >= buflen)) return -1; buf += ret; buflen -= ret; Med venlig hilsen / kind regards Morten Brørup CTO SmartShare Systems A/S Tonsbakken 16-18 DK-2740 Skovlunde Denmark Office +45 70 20 00 93 Direct +45 89 93 50 22 Mobile +45 25 40 82 12 m...@smartsharesystems.com <mailto:m...@smartsharesystems.com> www.smartsharesystems.com <https://www.smartsharesystems.com/>