Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. /*-
  2. * BSD LICENSE
  3. *
  4. * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of Intel Corporation nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33.  
  34. #include <stdint.h>
  35. #include <inttypes.h>
  36. #include <rte_eal.h>
  37. #include <rte_ethdev.h>
  38. #include <rte_cycles.h>
  39. #include <rte_lcore.h>
  40. #include <rte_mbuf.h>
  41.  
  42. #define RX_RING_SIZE 128
  43. #define TX_RING_SIZE 512
  44.  
  45. #define NUM_MBUFS 8191
  46. #define MBUF_CACHE_SIZE 250
  47. #define BURST_SIZE 1//32
  48.  
  49. static const struct rte_eth_conf port_conf_default = {
  50. .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
  51. };
  52.  
  53. /* basicfwd.c: Basic DPDK skeleton forwarding example. */
  54.  
  55. /*
  56. * Initializes a given port using global settings and with the RX buffers
  57. * coming from the mbuf_pool passed as a parameter.
  58. */
  59. static inline int
  60. port_init(uint8_t port, struct rte_mempool *mbuf_pool)
  61. {
  62. struct rte_eth_conf port_conf = port_conf_default;
  63. const uint16_t rx_rings = 1, tx_rings = 1;
  64. int retval;
  65. uint16_t q;
  66.  
  67. if (port >= rte_eth_dev_count())
  68. return -1;
  69.  
  70. /* Configure the Ethernet device. */
  71. retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
  72. if (retval != 0)
  73. return retval;
  74.  
  75. /* Allocate and set up 1 RX queue per Ethernet port. */
  76. for (q = 0; q < rx_rings; q++) {
  77. retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
  78. rte_eth_dev_socket_id(port), NULL, mbuf_pool);
  79. if (retval < 0)
  80. return retval;
  81. }
  82.  
  83. /* Allocate and set up 1 TX queue per Ethernet port. */
  84. for (q = 0; q < tx_rings; q++) {
  85. retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
  86. rte_eth_dev_socket_id(port), NULL);
  87. if (retval < 0)
  88. return retval;
  89. }
  90.  
  91. /* Start the Ethernet port. */
  92. retval = rte_eth_dev_start(port);
  93. if (retval < 0)
  94. return retval;
  95.  
  96. /* Display the port MAC address. */
  97. struct ether_addr addr;
  98. rte_eth_macaddr_get(port, &addr);
  99. printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
  100. " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
  101. (unsigned)port,
  102. addr.addr_bytes[0], addr.addr_bytes[1],
  103. addr.addr_bytes[2], addr.addr_bytes[3],
  104. addr.addr_bytes[4], addr.addr_bytes[5]);
  105.  
  106. return 0;
  107. }
  108.  
  109.  
  110.  
  111. struct payload
  112. {
  113. int type;
  114. int index;
  115. char msg[100];
  116. };
  117.  
  118. struct eth_header
  119. {
  120. char dst[6];
  121. char src[6];
  122. char protocol[2];
  123. };
  124.  
  125.  
  126.  
  127. /*
  128. * The lcore main. This is the main thread that does the work, reading from
  129. * an input port and writing to an output port.
  130. */
  131. static __attribute__((noreturn)) void
  132. lcore_main(void)
  133. {
  134. uint8_t src_port = 0, dst_port = 1;
  135. unsigned int /*ch_index, */data_len;
  136. unsigned int ready_pkts = 0;
  137. //unsigned int pkt_index;
  138. struct rte_mbuf *bufs;
  139. struct payload ready_payload;
  140. struct payload* payload_ref;
  141. struct payload* payload_loc;
  142. struct eth_header ready_eth_header;
  143. struct eth_header* eth_header_ref;
  144. struct eth_header* eth_header_loc;
  145. struct rte_mempool *mbuf_pool;
  146.  
  147. mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS,
  148. MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
  149.  
  150. if (mbuf_pool == NULL)
  151. {
  152. rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
  153. }
  154.  
  155. if (port_init(src_port, mbuf_pool) != 0)
  156. {
  157. rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
  158. src_port);
  159. }
  160.  
  161. if (port_init(dst_port, mbuf_pool) != 0)
  162. {
  163. rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
  164. dst_port);
  165. }
  166.  
  167.  
  168. /*
  169. * Check that the port is on the same NUMA node as the polling thread
  170. * for best performance.
  171. */
  172. if (rte_eth_dev_socket_id(src_port) > 0 &&
  173. rte_eth_dev_socket_id(src_port) !=
  174. (int)rte_socket_id())
  175. {
  176. printf("WARNING, port %u is on remote NUMA node to "
  177. "polling thread.\n\tPerformance will "
  178. "not be optimal.\n", src_port);
  179. }
  180.  
  181. printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
  182. rte_lcore_id());
  183.  
  184.  
  185.  
  186.  
  187. ready_payload.type = 4;
  188. ready_payload.index = 546;
  189. rte_memcpy(ready_payload.msg, "Read Me\n\0", 9);
  190.  
  191. ready_eth_header.dst[0] = 255;
  192. ready_eth_header.dst[1] = 255;
  193. ready_eth_header.dst[2] = 255;
  194. ready_eth_header.dst[3] = 255;
  195. ready_eth_header.dst[4] = 255;
  196. ready_eth_header.dst[5] = 255;
  197. ready_eth_header.src[0] = 0;
  198. ready_eth_header.src[1] = 0;
  199. ready_eth_header.src[2] = 0;
  200. ready_eth_header.src[3] = 0;
  201. ready_eth_header.src[4] = 0;
  202. ready_eth_header.src[5] = 1;
  203. ready_eth_header.protocol[0] = 8;
  204. ready_eth_header.protocol[1] = 0;
  205.  
  206. /* Run until the application is quit or killed. */
  207. for (;;)
  208. {
  209.  
  210.  
  211. bufs = rte_pktmbuf_alloc(mbuf_pool);
  212. payload_ref = &ready_payload;
  213. eth_header_ref = &ready_eth_header;
  214.  
  215. if (bufs != NULL)
  216. {
  217. eth_header_loc = rte_pktmbuf_append(bufs, sizeof(struct eth_header) + sizeof(struct payload));
  218. payload_loc = eth_header_loc + sizeof(struct eth_header);//rte_pktmbuf_append(bufs, sizeof(struct eth_header) + sizeof(struct payload));
  219. }
  220.  
  221. rte_memcpy(eth_header_loc, eth_header_ref, sizeof(struct eth_header));
  222. rte_memcpy(payload_loc, payload_ref, sizeof(struct payload));
  223.  
  224.  
  225.  
  226. //data_len = bufs[0]->data_len;
  227. //printf("rx = %d\ndata_len = %u\n", ready_pkts, data_len);
  228.  
  229.  
  230.  
  231.  
  232. /* Send burst of TX packets, to second port of pair. */
  233. const uint16_t nb_tx = rte_eth_tx_burst(dst_port, 0,
  234. &bufs, BURST_SIZE/*ready_pkts*/);
  235.  
  236.  
  237. /* Free any unsent packets. */
  238. //if (unlikely(nb_tx < ready_pkts)) {
  239. //uint16_t buf;
  240. //for (buf = nb_tx; buf < ready_pkts; buf++)
  241. //{
  242. //rte_pktmbuf_free(bufs);
  243. //}
  244. //}
  245.  
  246. /*if (bufs != NULL)
  247. {
  248. memset((bufs->buf_addr + 128), 0, sizeof(struct payload));
  249. ready_pkts = rte_eth_rx_burst(dst_port, 0,
  250. &bufs, BURST_SIZE);
  251.  
  252. data_len = bufs->data_len;
  253. printf("rx = %d\ndata_len = %u\n", ready_pkts, data_len);
  254. }
  255. else
  256. {
  257. printf("null\n");
  258. }*/
  259. //sleep(1);
  260.  
  261. }
  262. }
  263.  
  264. /*
  265. * The main function, which does initialization and calls the per-lcore
  266. * functions.
  267. */
  268. int
  269. main(int argc, char *argv[])
  270. {
  271. //struct rte_mempool *mbuf_pool;
  272. unsigned nb_ports;
  273. //uint8_t portid;
  274.  
  275. /* Initialize the Environment Abstraction Layer (EAL). */
  276. int ret = rte_eal_init(argc, argv);
  277. if (ret < 0)
  278. rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
  279.  
  280. argc -= ret;
  281. argv += ret;
  282.  
  283. /* Check that there is an even number of ports to send/receive on. */
  284. nb_ports = rte_eth_dev_count();
  285. if (nb_ports < 2 || (nb_ports & 1))
  286. rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
  287.  
  288. /* Creates a new mempool in memory to hold the mbufs. */
  289. // mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
  290. // MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
  291.  
  292. // if (mbuf_pool == NULL)
  293. // rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
  294.  
  295. /* Initialize all ports. */
  296. // for (portid = 0; portid < nb_ports; portid++)
  297. // if (port_init(portid, mbuf_pool) != 0)
  298. // rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
  299. // portid);
  300.  
  301. if (rte_lcore_count() > 1)
  302. printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
  303.  
  304. /* Call lcore_main on the master core only. */
  305. lcore_main();
  306.  
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement