Added rte_ring wrapper functions to support dpdk-17.05 and older version
[tldk.git] / examples / l4fwd / lcore.h
index d88e434..9deddcc 100644 (file)
 #ifndef LCORE_H_
 #define LCORE_H_
 
+#include <rte_random.h>
+
 #include "dpdk_legacy.h"
 
 /*
- * IPv6 destination lookup callback.
+ * IPv4 destination lookup callback.
  */
 static int
-lpm6_dst_lookup(void *data, const struct in6_addr *addr,
+lpm4_dst_lookup(void *data, const struct in_addr *addr,
        struct tle_dest *res)
 {
        int32_t rc;
-       uint8_t idx;
+       uint32_t idx;
        struct netbe_lcore *lc;
        struct tle_dest *dst;
-       uintptr_t p;
 
        lc = data;
-       p = (uintptr_t)addr->s6_addr;
 
-       rc = rte_lpm6_lookup(lc->lpm6, (uint8_t *)p, &idx);
+       rc = rte_lpm_lookup(lc->lpm4, rte_be_to_cpu_32(addr->s_addr), &idx);
        if (rc == 0) {
-               dst = &lc->dst6[idx];
+               dst = &lc->dst4[idx];
                rte_memcpy(res, dst, dst->l2_len + dst->l3_len +
                        offsetof(struct tle_dest, hdr));
        }
        return rc;
 }
 
+static int
+lcore_lpm_init(struct netbe_lcore *lc)
+{
+       int32_t sid;
+       char str[RTE_LPM_NAMESIZE];
+       const struct rte_lpm_config lpm4_cfg = {
+               .max_rules = MAX_RULES,
+               .number_tbl8s = MAX_TBL8,
+       };
+       const struct rte_lpm6_config lpm6_cfg = {
+               .max_rules = MAX_RULES,
+               .number_tbl8s = MAX_TBL8,
+       };
+
+       sid = rte_lcore_to_socket_id(lc->id);
+
+       snprintf(str, sizeof(str), "LPM4%u\n", lc->id);
+       lc->lpm4 = rte_lpm_create(str, sid, &lpm4_cfg);
+       RTE_LOG(NOTICE, USER1, "%s(lcore=%u): lpm4=%p;\n",
+               __func__, lc->id, lc->lpm4);
+       if (lc->lpm4 == NULL)
+               return -ENOMEM;
+
+       snprintf(str, sizeof(str), "LPM6%u\n", lc->id);
+       lc->lpm6 = rte_lpm6_create(str, sid, &lpm6_cfg);
+       RTE_LOG(NOTICE, USER1, "%s(lcore=%u): lpm6=%p;\n",
+               __func__, lc->id, lc->lpm6);
+       if (lc->lpm6 == NULL)
+               return -ENOMEM;
+
+       return 0;
+}
+
+/*
+ * Helper functions, finds BE by given local and remote addresses.
+ */
+static int
+netbe_find4(const struct in_addr *laddr, const uint16_t lport,
+       const struct in_addr *raddr, const uint32_t belc)
+{
+       uint32_t i, j;
+       uint32_t idx;
+       struct netbe_lcore *bc;
+
+       /* we have exactly one BE, use it for all traffic */
+       if (becfg.cpu_num == 1)
+               return 0;
+
+       /* search by provided be_lcore */
+       if (belc != LCORE_ID_ANY) {
+               for (i = 0; i != becfg.cpu_num; i++) {
+                       bc = becfg.cpu + i;
+                       if (belc == bc->id)
+                               return i;
+               }
+               RTE_LOG(NOTICE, USER1, "%s: no stream with be_lcore=%u\n",
+                       __func__, belc);
+               return -ENOENT;
+       }
+
+       /* search by local address */
+       if (laddr->s_addr != INADDR_ANY) {
+               for (i = 0; i != becfg.cpu_num; i++) {
+                       bc = becfg.cpu + i;
+                       /* search by queue for the local port */
+                       for (j = 0; j != bc->prtq_num; j++) {
+                               if (laddr->s_addr == bc->prtq[j].port.ipv4) {
+
+                                       if (lport == 0)
+                                               return i;
+
+                                       if (verify_queue_for_port(bc->prtq + j,
+                                                       lport) != 0)
+                                               return i;
+                               }
+                       }
+               }
+       }
+
+       /* search by remote address */
+       if (raddr->s_addr != INADDR_ANY) {
+               for (i = 0; i != becfg.cpu_num; i++) {
+                       bc = becfg.cpu + i;
+                       if (rte_lpm_lookup(bc->lpm4,
+                                       rte_be_to_cpu_32(raddr->s_addr),
+                                       &idx) == 0) {
+
+                               if (lport == 0)
+                                       return i;
+
+                               /* search by queue for the local port */
+                               for (j = 0; j != bc->prtq_num; j++)
+                                       if (verify_queue_for_port(bc->prtq + j,
+                                                       lport) != 0)
+                                               return i;
+                       }
+               }
+       }
+
+       return -ENOENT;
+}
+
 static int
 create_context(struct netbe_lcore *lc, const struct tle_ctx_param *ctx_prm)
 {
@@ -64,6 +166,11 @@ create_context(struct netbe_lcore *lc, const struct tle_ctx_param *ctx_prm)
                cprm.lookup4_data = lc;
                cprm.lookup6 = lpm6_dst_lookup;
                cprm.lookup6_data = lc;
+               if (cprm.secret_key.u64[0] == 0 &&
+                       cprm.secret_key.u64[1] == 0) {
+                       cprm.secret_key.u64[0] = rte_rand();
+                       cprm.secret_key.u64[1] = rte_rand();
+               }
 
                frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) /
                                                MS_PER_S * FRAG_TTL;
@@ -204,73 +311,6 @@ netfe_lcore_cmp(const void *s1, const void *s2)
        return p1->lcore - p2->lcore;
 }
 
-static int
-netbe_find6(const struct in6_addr *laddr, uint16_t lport,
-       const struct in6_addr *raddr, uint32_t belc)
-{
-       uint32_t i, j;
-       uint8_t idx;
-       struct netbe_lcore *bc;
-
-       /* we have exactly one BE, use it for all traffic */
-       if (becfg.cpu_num == 1)
-               return 0;
-
-       /* search by provided be_lcore */
-       if (belc != LCORE_ID_ANY) {
-               for (i = 0; i != becfg.cpu_num; i++) {
-                       bc = becfg.cpu + i;
-                       if (belc == bc->id)
-                               return i;
-               }
-               RTE_LOG(NOTICE, USER1, "%s: no stream with belcore=%u\n",
-                       __func__, belc);
-               return -ENOENT;
-       }
-
-       /* search by local address */
-       if (memcmp(laddr, &in6addr_any, sizeof(*laddr)) != 0) {
-               for (i = 0; i != becfg.cpu_num; i++) {
-                       bc = becfg.cpu + i;
-                       /* search by queue for the local port */
-                       for (j = 0; j != bc->prtq_num; j++) {
-                               if (memcmp(laddr, &bc->prtq[j].port.ipv6,
-                                               sizeof(*laddr)) == 0) {
-
-                                       if (lport == 0)
-                                               return i;
-
-                                       if (verify_queue_for_port(bc->prtq + j,
-                                                       lport) != 0)
-                                               return i;
-                               }
-                       }
-               }
-       }
-
-       /* search by remote address */
-       if (memcmp(raddr, &in6addr_any, sizeof(*raddr)) == 0) {
-               for (i = 0; i != becfg.cpu_num; i++) {
-                       bc = becfg.cpu + i;
-                       if (rte_lpm6_lookup(bc->lpm6,
-                                       (uint8_t *)(uintptr_t)raddr->s6_addr,
-                                       &idx) == 0) {
-
-                               if (lport == 0)
-                                       return i;
-
-                               /* search by queue for the local port */
-                               for (j = 0; j != bc->prtq_num; j++)
-                                       if (verify_queue_for_port(bc->prtq + j,
-                                                       lport) != 0)
-                                               return i;
-                       }
-               }
-       }
-
-       return -ENOENT;
-}
-
 static int
 netbe_find(const struct sockaddr_storage *la,
        const struct sockaddr_storage *ra,