4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <rte_common.h>
38 #include <rte_byteorder.h>
40 #include <rte_malloc.h>
41 #include <rte_ethdev.h>
43 #include <rte_ether.h>
45 #include <rte_jhash.h>
47 #include <rte_port_ethdev.h>
48 #include <rte_port_ring.h>
49 #include <rte_table_stub.h>
50 #include <rte_pipeline.h>
55 struct app_core_rx_message_handle_params {
56 struct rte_ring *ring_req;
57 struct rte_ring *ring_resp;
59 struct rte_pipeline *p;
64 app_message_handle(struct app_core_rx_message_handle_params *params);
67 app_pipeline_rx_port_in_action_handler(struct rte_mbuf **pkts, uint32_t n,
68 uint64_t *pkts_mask, void *arg);
71 app_main_loop_pipeline_rx(void) {
72 struct rte_pipeline *p;
73 uint32_t port_in_id[APP_MAX_PORTS];
74 uint32_t port_out_id[APP_MAX_PORTS];
75 uint32_t table_id[APP_MAX_PORTS];
78 uint32_t core_id = rte_lcore_id();
79 struct app_core_params *core_params = app_get_core_params(core_id);
80 struct app_core_rx_message_handle_params mh_params;
82 if ((core_params == NULL) || (core_params->core_type != APP_CORE_RX))
83 rte_panic("Core %u misconfiguration\n", core_id);
85 RTE_LOG(INFO, USER1, "Core %u is doing RX\n", core_id);
87 /* Pipeline configuration */
88 struct rte_pipeline_params pipeline_params = {
90 .socket_id = rte_socket_id(),
93 p = rte_pipeline_create(&pipeline_params);
95 rte_panic("%s: Unable to configure the pipeline\n", __func__);
97 /* Input port configuration */
98 for (i = 0; i < app.n_ports; i++) {
99 struct rte_port_ethdev_reader_params port_ethdev_params = {
100 .port_id = app.ports[i],
104 struct rte_pipeline_port_in_params port_params = {
105 .ops = &rte_port_ethdev_reader_ops,
106 .arg_create = (void *) &port_ethdev_params,
107 .f_action = app_pipeline_rx_port_in_action_handler,
109 .burst_size = app.bsz_hwq_rd,
112 if (rte_pipeline_port_in_create(p, &port_params,
114 rte_panic("%s: Unable to configure input port for "
115 "port %d\n", __func__, app.ports[i]);
118 /* Output port configuration */
119 for (i = 0; i < app.n_ports; i++) {
120 struct rte_port_ring_writer_params port_ring_params = {
121 .ring = app.rings[core_params->swq_out[i]],
122 .tx_burst_sz = app.bsz_swq_wr,
125 struct rte_pipeline_port_out_params port_params = {
126 .ops = &rte_port_ring_writer_ops,
127 .arg_create = (void *) &port_ring_params,
129 .f_action_bulk = NULL,
133 if (rte_pipeline_port_out_create(p, &port_params,
135 rte_panic("%s: Unable to configure output port for "
136 "ring RX %i\n", __func__, i);
139 /* Table configuration */
140 for (i = 0; i < app.n_ports; i++) {
141 struct rte_pipeline_table_params table_params = {
142 .ops = &rte_table_stub_ops,
144 .f_action_hit = NULL,
145 .f_action_miss = NULL,
147 .action_data_size = 0,
150 if (rte_pipeline_table_create(p, &table_params, &table_id[i]))
151 rte_panic("%s: Unable to configure table %u\n",
152 __func__, table_id[i]);
155 /* Interconnecting ports and tables */
156 for (i = 0; i < app.n_ports; i++)
157 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
159 rte_panic("%s: Unable to connect input port %u to "
160 "table %u\n", __func__, port_in_id[i],
163 /* Add entries to tables */
164 for (i = 0; i < app.n_ports; i++) {
165 struct rte_pipeline_table_entry default_entry = {
166 .action = RTE_PIPELINE_ACTION_PORT,
167 {.port_id = port_out_id[i]},
170 struct rte_pipeline_table_entry *default_entry_ptr;
172 if (rte_pipeline_table_default_entry_add(p, table_id[i],
173 &default_entry, &default_entry_ptr))
174 rte_panic("%s: Unable to add default entry to "
175 "table %u\n", __func__, table_id[i]);
178 /* Enable input ports */
179 for (i = 0; i < app.n_ports; i++)
180 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
181 rte_panic("Unable to enable input port %u\n",
184 /* Check pipeline consistency */
185 if (rte_pipeline_check(p) < 0)
186 rte_panic("%s: Pipeline consistency check failed\n", __func__);
188 /* Message handling */
190 app_get_ring_req(app_get_first_core_id(APP_CORE_RX));
191 mh_params.ring_resp =
192 app_get_ring_resp(app_get_first_core_id(APP_CORE_RX));
194 mh_params.port_in_id = port_in_id;
200 if ((i & APP_FLUSH) == 0) {
201 rte_pipeline_flush(p);
202 app_message_handle(&mh_params);
209 __attribute__((unused)) uint32_t key_size,
210 __attribute__((unused)) uint64_t seed)
212 struct app_flow_key *flow_key = (struct app_flow_key *) key;
213 uint32_t ip_dst = rte_be_to_cpu_32(flow_key->ip_dst);
214 uint64_t signature = (ip_dst & 0x00FFFFFFLLU) >> 2;
220 rte_jhash2_16(uint32_t *k, uint32_t initval)
224 a = b = RTE_JHASH_GOLDEN_RATIO;
230 __rte_jhash_mix(a, b, c);
232 c += 16; /* length in bytes */
233 a += k[3]; /* Remaining word */
235 __rte_jhash_mix(a, b, c);
241 app_pkt_metadata_fill(struct rte_mbuf *m)
243 uint8_t *m_data = rte_pktmbuf_mtod(m, uint8_t *);
244 struct app_pkt_metadata *c =
245 (struct app_pkt_metadata *) RTE_MBUF_METADATA_UINT8_PTR(m, 0);
246 struct ipv4_hdr *ip_hdr =
247 (struct ipv4_hdr *) &m_data[sizeof(struct ether_hdr)];
248 uint64_t *ipv4_hdr_slab = (uint64_t *) ip_hdr;
250 /* TTL and Header Checksum are set to 0 */
251 c->flow_key.slab0 = ipv4_hdr_slab[1] & 0xFFFFFFFF0000FF00LLU;
252 c->flow_key.slab1 = ipv4_hdr_slab[2];
253 c->signature = test_hash((void *) &c->flow_key, 0, 0);
255 /* Pop Ethernet header */
256 if (app.ether_hdr_pop_push) {
257 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
259 m->l3_len = sizeof(struct ipv4_hdr);
264 app_pipeline_rx_port_in_action_handler(
265 struct rte_mbuf **pkts,
268 __rte_unused void *arg)
272 for (i = 0; i < n; i++) {
273 struct rte_mbuf *m = pkts[i];
275 app_pkt_metadata_fill(m);
278 *pkts_mask = (~0LLU) >> (64 - n);
284 app_main_loop_rx(void) {
285 struct app_mbuf_array *ma;
289 uint32_t core_id = rte_lcore_id();
290 struct app_core_params *core_params = app_get_core_params(core_id);
292 if ((core_params == NULL) || (core_params->core_type != APP_CORE_RX))
293 rte_panic("Core %u misconfiguration\n", core_id);
295 RTE_LOG(INFO, USER1, "Core %u is doing RX (no pipeline)\n", core_id);
297 ma = rte_malloc_socket(NULL, sizeof(struct app_mbuf_array),
298 RTE_CACHE_LINE_SIZE, rte_socket_id());
300 rte_panic("%s: cannot allocate buffer space\n", __func__);
302 for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
305 n_mbufs = rte_eth_rx_burst(
314 for (j = 0; j < n_mbufs; j++) {
315 struct rte_mbuf *m = ma->array[j];
317 app_pkt_metadata_fill(m);
321 ret = rte_ring_sp_enqueue_bulk(
322 app.rings[core_params->swq_out[i]],
330 app_message_handle(struct app_core_rx_message_handle_params *params)
332 struct rte_ring *ring_req = params->ring_req;
333 struct rte_ring *ring_resp;
335 struct app_msg_req *req;
336 struct app_msg_resp *resp;
337 struct rte_pipeline *p;
338 uint32_t *port_in_id;
341 /* Read request message */
342 result = rte_ring_sc_dequeue(ring_req, &msg);
346 ring_resp = params->ring_resp;
348 port_in_id = params->port_in_id;
351 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
353 case APP_MSG_REQ_PING:
359 case APP_MSG_REQ_RX_PORT_ENABLE:
361 result = rte_pipeline_port_in_enable(p,
362 port_in_id[req->rx_up.port]);
366 case APP_MSG_REQ_RX_PORT_DISABLE:
368 result = rte_pipeline_port_in_disable(p,
369 port_in_id[req->rx_down.port]);
374 rte_panic("RX Unrecognized message type (%u)\n", req->type);
377 /* Fill in response message */
378 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
379 resp->result = result;
383 result = rte_ring_sp_enqueue(ring_resp, msg);
384 } while (result == -ENOBUFS);