Imported Upstream version 16.07-rc3
[deb_dpdk.git] / examples / l2fwd-crypto / main.c
index 8dc616d..dd39cc1 100644 (file)
@@ -45,6 +45,8 @@
 #include <ctype.h>
 #include <errno.h>
 #include <getopt.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include <rte_atomic.h>
 #include <rte_branch_prediction.h>
@@ -243,7 +245,7 @@ struct l2fwd_crypto_statistics {
 } __rte_cache_aligned;
 
 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
-struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
+struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
 
 /* A tsc-based timer responsible for triggering statistics printout */
 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
@@ -588,10 +590,18 @@ l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
 static void
 generate_random_key(uint8_t *key, unsigned length)
 {
-       unsigned i;
+       int fd;
+       int ret;
+
+       fd = open("/dev/urandom", O_RDONLY);
+       if (fd < 0)
+               rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
+
+       ret = read(fd, key, length);
+       close(fd);
 
-       for (i = 0; i < length; i++)
-               key[i] = rand() % 0xff;
+       if (ret != (signed)length)
+               rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
 }
 
 static struct rte_cryptodev_sym_session *
@@ -628,7 +638,7 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
 
        unsigned lcore_id = rte_lcore_id();
        uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
-       unsigned i, j, portid, nb_rx;
+       unsigned i, j, portid, nb_rx, len;
        struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
        const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
                        US_PER_S * BURST_TX_DRAIN_US;
@@ -727,10 +737,18 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                cur_tsc = rte_rdtsc();
 
                /*
-                * TX burst queue drain
+                * Crypto device/TX burst queue drain
                 */
                diff_tsc = cur_tsc - prev_tsc;
                if (unlikely(diff_tsc > drain_tsc)) {
+                       /* Enqueue all crypto ops remaining in buffers */
+                       for (i = 0; i < qconf->nb_crypto_devs; i++) {
+                               cparams = &port_cparams[i];
+                               len = qconf->op_buf[cparams->dev_id].len;
+                               l2fwd_crypto_send_burst(qconf, len, cparams);
+                               qconf->op_buf[cparams->dev_id].len = 0;
+                       }
+                       /* Transmit all packets remaining in buffers */
                        for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
                                if (qconf->pkt_buf[portid].len == 0)
                                        continue;
@@ -1187,8 +1205,6 @@ l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
 static void
 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
 {
-       srand(time(NULL));
-
        options->portmask = 0xffffffff;
        options->nb_ports_per_lcore = 1;
        options->refresh_period = 10000;