examples/l2fwd-crypto: fix possible out-of-bounds
[dpdk.git] / examples / l2fwd-crypto / main.c
index 8d4a10a..f020be3 100644 (file)
@@ -193,6 +193,8 @@ struct l2fwd_crypto_options {
        char string_type[MAX_STR_LEN];
 
        uint64_t cryptodev_mask;
+
+       unsigned int mac_updating;
 };
 
 /** l2fwd crypto lcore params */
@@ -608,21 +610,31 @@ l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
 }
 
 static void
-l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
+l2fwd_mac_updating(struct rte_mbuf *m, unsigned int dest_portid)
 {
        struct ether_hdr *eth;
        void *tmp;
-       unsigned dst_port;
 
-       dst_port = l2fwd_dst_ports[portid];
        eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
 
        /* 02:00:00:00:00:xx */
        tmp = &eth->d_addr.addr_bytes[0];
-       *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
+       *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
 
        /* src addr */
-       ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
+       ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
+}
+
+static void
+l2fwd_simple_forward(struct rte_mbuf *m, unsigned int portid,
+               struct l2fwd_crypto_options *options)
+{
+       unsigned int dst_port;
+
+       dst_port = l2fwd_dst_ports[portid];
+
+       if (options->mac_updating)
+               l2fwd_mac_updating(m, dst_port);
 
        l2fwd_send_packet(m, (uint8_t) dst_port);
 }
@@ -650,7 +662,12 @@ initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
 {
        struct rte_crypto_sym_xform *first_xform;
        struct rte_cryptodev_sym_session *session;
-       uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
+       int retval = rte_cryptodev_socket_id(cdev_id);
+
+       if (retval < 0)
+               return NULL;
+
+       uint8_t socket_id = (uint8_t) retval;
        struct rte_mempool *sess_mp = session_pool_socket[socket_id];
 
        if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
@@ -927,7 +944,8 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                                        m = ops_burst[j]->sym->m_src;
 
                                        rte_crypto_op_free(ops_burst[j]);
-                                       l2fwd_simple_forward(m, portid);
+                                       l2fwd_simple_forward(m, portid,
+                                                       options);
                                }
                        } while (nb_rx == MAX_PKT_BURST);
                }
@@ -982,7 +1000,12 @@ l2fwd_crypto_usage(const char *prgname)
                "  --digest_size SIZE: size of digest to be generated/verified\n"
 
                "  --sessionless\n"
-               "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
+               "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
+
+               "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
+               "      When enabled:\n"
+               "       - The source MAC address is replaced by the TX port MAC address\n"
+               "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
               prgname);
 }
 
@@ -1329,6 +1352,16 @@ l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
        else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
                return parse_cryptodev_mask(options, optarg);
 
+       else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
+               options->mac_updating = 1;
+               return 0;
+       }
+
+       else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
+               options->mac_updating = 0;
+               return 0;
+       }
+
        return -1;
 }
 
@@ -1462,6 +1495,8 @@ l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
 
        options->type = CDEV_TYPE_ANY;
        options->cryptodev_mask = UINT64_MAX;
+
+       options->mac_updating = 1;
 }
 
 static void
@@ -1623,6 +1658,9 @@ l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
                        { "sessionless", no_argument, 0, 0 },
                        { "cryptodev_mask", required_argument, 0, 0},
 
+                       { "mac-updating", no_argument, 0, 0},
+                       { "no-mac-updating", no_argument, 0, 0},
+
                        { NULL, 0, 0, 0 }
        };
 
@@ -1963,7 +2001,14 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                        cdev_id++) {
                struct rte_cryptodev_qp_conf qp_conf;
                struct rte_cryptodev_info dev_info;
-               uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
+               retval = rte_cryptodev_socket_id(cdev_id);
+
+               if (retval < 0) {
+                       printf("Invalid crypto device id used\n");
+                       return -1;
+               }
+
+               uint8_t socket_id = (uint8_t) retval;
 
                struct rte_cryptodev_config conf = {
                        .nb_queue_pairs = 1,
@@ -2445,6 +2490,9 @@ main(int argc, char **argv)
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
 
+       printf("MAC updating %s\n",
+                       options.mac_updating ? "enabled" : "disabled");
+
        /* create the mbuf pool */
        l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
                        sizeof(struct rte_crypto_op),