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.
38 #include <rte_malloc.h>
40 #include <rte_ethdev.h>
41 #include <rte_ether.h>
43 #include <rte_byteorder.h>
45 #include <rte_port_ring.h>
46 #include <rte_table_lpm.h>
47 #include <rte_table_hash.h>
48 #include <rte_pipeline.h>
54 struct app_routing_table_entry {
55 struct rte_pipeline_table_entry head;
60 struct app_arp_table_entry {
61 struct rte_pipeline_table_entry head;
62 struct ether_addr nh_arp;
66 app_routing_table_write_metadata(
68 struct app_routing_table_entry *entry)
70 struct app_pkt_metadata *c =
71 (struct app_pkt_metadata *) RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
73 c->arp_key.nh_ip = entry->nh_ip;
74 c->arp_key.nh_iface = entry->nh_iface;
79 struct rte_mbuf **pkts,
81 struct rte_pipeline_table_entry **entries,
82 __attribute__((unused)) void *arg)
84 uint64_t pkts_in_mask = *pkts_mask;
86 if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {
87 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);
90 for (i = 0; i < n_pkts; i++) {
91 struct rte_mbuf *m = pkts[i];
92 struct app_routing_table_entry *a =
93 (struct app_routing_table_entry *) entries[i];
95 app_routing_table_write_metadata(m, a);
98 for ( ; pkts_in_mask; ) {
100 struct app_routing_table_entry *a;
102 uint32_t packet_index;
104 packet_index = __builtin_ctzll(pkts_in_mask);
105 pkt_mask = 1LLU << packet_index;
106 pkts_in_mask &= ~pkt_mask;
108 m = pkts[packet_index];
109 a = (struct app_routing_table_entry *)
110 entries[packet_index];
111 app_routing_table_write_metadata(m, a);
118 app_arp_table_write_metadata(
119 struct rte_mbuf *pkt,
120 struct app_arp_table_entry *entry)
122 struct app_pkt_metadata *c =
123 (struct app_pkt_metadata *) RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
124 ether_addr_copy(&entry->nh_arp, &c->nh_arp);
129 struct rte_mbuf **pkts,
131 struct rte_pipeline_table_entry **entries,
132 __attribute__((unused)) void *arg)
134 uint64_t pkts_in_mask = *pkts_mask;
136 if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {
137 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);
140 for (i = 0; i < n_pkts; i++) {
141 struct rte_mbuf *m = pkts[i];
142 struct app_arp_table_entry *a =
143 (struct app_arp_table_entry *) entries[i];
145 app_arp_table_write_metadata(m, a);
148 for ( ; pkts_in_mask; ) {
150 struct app_arp_table_entry *a;
152 uint32_t packet_index;
154 packet_index = __builtin_ctzll(pkts_in_mask);
155 pkt_mask = 1LLU << packet_index;
156 pkts_in_mask &= ~pkt_mask;
158 m = pkts[packet_index];
159 a = (struct app_arp_table_entry *)
160 entries[packet_index];
161 app_arp_table_write_metadata(m, a);
168 static uint64_t app_arp_table_hash(
170 __attribute__((unused)) uint32_t key_size,
171 __attribute__((unused)) uint64_t seed)
173 uint32_t *k = (uint32_t *) key;
178 struct app_core_routing_message_handle_params {
179 struct rte_ring *ring_req;
180 struct rte_ring *ring_resp;
181 struct rte_pipeline *p;
182 uint32_t *port_out_id;
183 uint32_t routing_table_id;
184 uint32_t arp_table_id;
188 app_message_handle(struct app_core_routing_message_handle_params *params);
191 app_main_loop_pipeline_routing(void) {
192 struct rte_pipeline_params pipeline_params = {
194 .socket_id = rte_socket_id(),
197 struct rte_pipeline *p;
198 uint32_t port_in_id[APP_MAX_PORTS];
199 uint32_t port_out_id[APP_MAX_PORTS];
200 uint32_t routing_table_id, arp_table_id;
203 uint32_t core_id = rte_lcore_id();
204 struct app_core_params *core_params = app_get_core_params(core_id);
205 struct app_core_routing_message_handle_params mh_params;
207 if ((core_params == NULL) || (core_params->core_type != APP_CORE_RT))
208 rte_panic("Core %u misconfiguration\n", core_id);
210 RTE_LOG(INFO, USER1, "Core %u is doing routing\n", core_id);
212 /* Pipeline configuration */
213 p = rte_pipeline_create(&pipeline_params);
215 rte_panic("Unable to configure the pipeline\n");
217 /* Input port configuration */
218 for (i = 0; i < app.n_ports; i++) {
219 struct rte_port_ring_reader_params port_ring_params = {
220 .ring = app.rings[core_params->swq_in[i]],
223 struct rte_pipeline_port_in_params port_params = {
224 .ops = &rte_port_ring_reader_ops,
225 .arg_create = (void *) &port_ring_params,
228 .burst_size = app.bsz_swq_rd,
231 if (rte_pipeline_port_in_create(p, &port_params,
233 rte_panic("Unable to configure input port for "
237 /* Output port configuration */
238 for (i = 0; i < app.n_ports; i++) {
239 struct rte_port_ring_writer_params port_ring_params = {
240 .ring = app.rings[core_params->swq_out[i]],
241 .tx_burst_sz = app.bsz_swq_wr,
244 struct rte_pipeline_port_out_params port_params = {
245 .ops = &rte_port_ring_writer_ops,
246 .arg_create = (void *) &port_ring_params,
248 .f_action_bulk = NULL,
252 if (rte_pipeline_port_out_create(p, &port_params,
254 rte_panic("Unable to configure output port for "
258 /* Routing table configuration */
260 struct rte_table_lpm_params table_lpm_params = {
261 .n_rules = app.max_routing_rules,
263 sizeof(struct app_routing_table_entry),
264 .offset = __builtin_offsetof(struct app_pkt_metadata,
268 struct rte_pipeline_table_params table_params = {
269 .ops = &rte_table_lpm_ops,
270 .arg_create = &table_lpm_params,
271 .f_action_hit = app_routing_table_ah,
272 .f_action_miss = NULL,
275 sizeof(struct app_routing_table_entry) -
276 sizeof(struct rte_pipeline_table_entry),
279 if (rte_pipeline_table_create(p, &table_params,
281 rte_panic("Unable to configure the LPM table\n");
284 /* ARP table configuration */
286 struct rte_table_hash_key8_lru_params table_arp_params = {
287 .n_entries = app.max_arp_rules,
288 .f_hash = app_arp_table_hash,
290 .signature_offset = 0, /* Unused */
291 .key_offset = __builtin_offsetof(
292 struct app_pkt_metadata, arp_key),
295 struct rte_pipeline_table_params table_params = {
296 .ops = &rte_table_hash_key8_lru_dosig_ops,
297 .arg_create = &table_arp_params,
298 .f_action_hit = app_arp_table_ah,
299 .f_action_miss = NULL,
301 .action_data_size = sizeof(struct app_arp_table_entry) -
302 sizeof(struct rte_pipeline_table_entry),
305 if (rte_pipeline_table_create(p, &table_params, &arp_table_id))
306 rte_panic("Unable to configure the ARP table\n");
309 /* Interconnecting ports and tables */
310 for (i = 0; i < app.n_ports; i++) {
311 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
313 rte_panic("Unable to connect input port %u to "
314 "table %u\n", port_in_id[i], routing_table_id);
317 /* Enable input ports */
318 for (i = 0; i < app.n_ports; i++)
319 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
320 rte_panic("Unable to enable input port %u\n",
323 /* Check pipeline consistency */
324 if (rte_pipeline_check(p) < 0)
325 rte_panic("Pipeline consistency check failed\n");
327 /* Message handling */
329 app_get_ring_req(app_get_first_core_id(APP_CORE_RT));
330 mh_params.ring_resp =
331 app_get_ring_resp(app_get_first_core_id(APP_CORE_RT));
333 mh_params.port_out_id = port_out_id;
334 mh_params.routing_table_id = routing_table_id;
335 mh_params.arp_table_id = arp_table_id;
341 if ((i & APP_FLUSH) == 0) {
342 rte_pipeline_flush(p);
343 app_message_handle(&mh_params);
349 app_message_handle(struct app_core_routing_message_handle_params *params)
351 struct rte_ring *ring_req = params->ring_req;
352 struct rte_ring *ring_resp;
354 struct app_msg_req *req;
355 struct app_msg_resp *resp;
356 struct rte_pipeline *p;
357 uint32_t *port_out_id;
358 uint32_t routing_table_id, arp_table_id;
361 /* Read request message */
362 result = rte_ring_sc_dequeue(ring_req, &msg);
366 ring_resp = params->ring_resp;
368 port_out_id = params->port_out_id;
369 routing_table_id = params->routing_table_id;
370 arp_table_id = params->arp_table_id;
373 req = (struct app_msg_req *) ((struct rte_mbuf *)msg)->ctrl.data;
375 case APP_MSG_REQ_PING:
381 case APP_MSG_REQ_RT_ADD:
383 struct app_routing_table_entry entry = {
385 .action = RTE_PIPELINE_ACTION_TABLE,
386 {.table_id = arp_table_id},
388 .nh_ip = req->routing_add.nh_ip,
389 .nh_iface = port_out_id[req->routing_add.port],
392 struct rte_table_lpm_key key = {
393 .ip = req->routing_add.ip,
394 .depth = req->routing_add.depth,
397 struct rte_pipeline_table_entry *entry_ptr;
401 result = rte_pipeline_table_entry_add(p, routing_table_id, &key,
402 (struct rte_pipeline_table_entry *) &entry, &key_found,
407 case APP_MSG_REQ_RT_DEL:
409 struct rte_table_lpm_key key = {
410 .ip = req->routing_del.ip,
411 .depth = req->routing_del.depth,
416 result = rte_pipeline_table_entry_delete(p, routing_table_id,
417 &key, &key_found, NULL);
421 case APP_MSG_REQ_ARP_ADD:
424 struct app_arp_table_entry entry = {
426 .action = RTE_PIPELINE_ACTION_PORT,
428 port_out_id[req->arp_add.out_iface]},
430 .nh_arp = req->arp_add.nh_arp,
433 struct app_arp_key arp_key = {
434 .nh_ip = req->arp_add.nh_ip,
435 .nh_iface = port_out_id[req->arp_add.out_iface],
438 struct rte_pipeline_table_entry *entry_ptr;
442 result = rte_pipeline_table_entry_add(p, arp_table_id, &arp_key,
443 (struct rte_pipeline_table_entry *) &entry, &key_found,
448 case APP_MSG_REQ_ARP_DEL:
450 struct app_arp_key arp_key = {
451 .nh_ip = req->arp_del.nh_ip,
452 .nh_iface = port_out_id[req->arp_del.out_iface],
457 result = rte_pipeline_table_entry_delete(p, arp_table_id,
458 &arp_key, &key_found, NULL);
463 rte_panic("RT Unrecognized message type (%u)\n", req->type);
466 /* Fill in response message */
467 resp = (struct app_msg_resp *) ((struct rte_mbuf *)msg)->ctrl.data;
468 resp->result = result;
472 result = rte_ring_sp_enqueue(ring_resp, msg);
473 } while (result == -ENOBUFS);