examples/ip_pipeline: add hash key mask parameter
[dpdk.git] / examples / ip_pipeline / pipeline / pipeline_flow_classification_be.c
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 <string.h>
35
36 #include <rte_common.h>
37 #include <rte_malloc.h>
38 #include <rte_table_hash.h>
39 #include <rte_byteorder.h>
40 #include <pipeline.h>
41
42 #include "pipeline_flow_classification_be.h"
43 #include "hash_func.h"
44
45 struct pipeline_flow_classification {
46         struct pipeline p;
47         pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
48
49         uint32_t n_flows;
50         uint32_t key_offset;
51         uint32_t key_size;
52         uint32_t hash_offset;
53         uint8_t *key_mask;
54 } __rte_cache_aligned;
55
56 static void *
57 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
58
59 static pipeline_msg_req_handler handlers[] = {
60         [PIPELINE_MSG_REQ_PING] =
61                 pipeline_msg_req_ping_handler,
62         [PIPELINE_MSG_REQ_STATS_PORT_IN] =
63                 pipeline_msg_req_stats_port_in_handler,
64         [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
65                 pipeline_msg_req_stats_port_out_handler,
66         [PIPELINE_MSG_REQ_STATS_TABLE] =
67                 pipeline_msg_req_stats_table_handler,
68         [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
69                 pipeline_msg_req_port_in_enable_handler,
70         [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
71                 pipeline_msg_req_port_in_disable_handler,
72         [PIPELINE_MSG_REQ_CUSTOM] =
73                 pipeline_fc_msg_req_custom_handler,
74 };
75
76 static void *
77 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
78
79 static void *
80 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
81
82 static void *
83 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
84
85 static void *
86 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
87
88 static void *
89 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
90
91 static pipeline_msg_req_handler custom_handlers[] = {
92         [PIPELINE_FC_MSG_REQ_FLOW_ADD] =
93                 pipeline_fc_msg_req_add_handler,
94         [PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
95                 pipeline_fc_msg_req_add_bulk_handler,
96         [PIPELINE_FC_MSG_REQ_FLOW_DEL] =
97                 pipeline_fc_msg_req_del_handler,
98         [PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
99                 pipeline_fc_msg_req_add_default_handler,
100         [PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
101                 pipeline_fc_msg_req_del_default_handler,
102 };
103
104 /*
105  * Flow table
106  */
107 struct flow_table_entry {
108         struct rte_pipeline_table_entry head;
109 };
110
111 rte_table_hash_op_hash hash_func[] = {
112         hash_default_key8,
113         hash_default_key16,
114         hash_default_key24,
115         hash_default_key32,
116         hash_default_key40,
117         hash_default_key48,
118         hash_default_key56,
119         hash_default_key64
120 };
121
122 static int
123 pipeline_fc_parse_args(struct pipeline_flow_classification *p,
124         struct pipeline_params *params)
125 {
126         uint32_t n_flows_present = 0;
127         uint32_t key_offset_present = 0;
128         uint32_t key_size_present = 0;
129         uint32_t hash_offset_present = 0;
130         uint32_t key_mask_present = 0;
131
132         uint32_t i;
133         char *key_mask_str = NULL;
134
135         p->hash_offset = 0;
136
137         for (i = 0; i < params->n_args; i++) {
138                 char *arg_name = params->args_name[i];
139                 char *arg_value = params->args_value[i];
140
141                 /* n_flows */
142                 if (strcmp(arg_name, "n_flows") == 0) {
143                         if (n_flows_present)
144                                 return -1;
145                         n_flows_present = 1;
146
147                         p->n_flows = atoi(arg_value);
148                         if (p->n_flows == 0)
149                                 return -1;
150
151                         continue;
152                 }
153
154                 /* key_offset */
155                 if (strcmp(arg_name, "key_offset") == 0) {
156                         if (key_offset_present)
157                                 return -1;
158                         key_offset_present = 1;
159
160                         p->key_offset = atoi(arg_value);
161
162                         continue;
163                 }
164
165                 /* key_size */
166                 if (strcmp(arg_name, "key_size") == 0) {
167                         if (key_size_present)
168                                 return -1;
169                         key_size_present = 1;
170
171                         p->key_size = atoi(arg_value);
172                         if ((p->key_size == 0) ||
173                                 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
174                                 (p->key_size % 8))
175                                 return -1;
176
177                         continue;
178                 }
179
180                 /* key_mask */
181                 if (strcmp(arg_name, "key_mask") == 0) {
182                         if (key_mask_present)
183                                 return -1;
184
185                         key_mask_str = strdup(arg_value);
186                         if (key_mask_str == NULL)
187                                 return -1;
188
189                         key_mask_present = 1;
190
191                         continue;
192                 }
193
194                 /* hash_offset */
195                 if (strcmp(arg_name, "hash_offset") == 0) {
196                         if (hash_offset_present)
197                                 return -1;
198                         hash_offset_present = 1;
199
200                         p->hash_offset = atoi(arg_value);
201
202                         continue;
203                 }
204
205                 /* Unknown argument */
206                 return -1;
207         }
208
209         /* Check that mandatory arguments are present */
210         if ((n_flows_present == 0) ||
211                 (key_offset_present == 0) ||
212                 (key_size_present == 0))
213                 return -1;
214
215         if (key_mask_present) {
216                 p->key_mask = rte_malloc(NULL, p->key_size, 0);
217                 if (p->key_mask == NULL)
218                         return -1;
219
220                 if (parse_hex_string(key_mask_str, p->key_mask, &p->key_size)
221                         != 0) {
222                         free(p->key_mask);
223                         return -1;
224                 }
225
226                 free(key_mask_str);
227         }
228
229         return 0;
230 }
231
232 static void *pipeline_fc_init(struct pipeline_params *params,
233         __rte_unused void *arg)
234 {
235         struct pipeline *p;
236         struct pipeline_flow_classification *p_fc;
237         uint32_t size, i;
238
239         /* Check input arguments */
240         if (params == NULL)
241                 return NULL;
242
243         /* Memory allocation */
244         size = RTE_CACHE_LINE_ROUNDUP(
245                 sizeof(struct pipeline_flow_classification));
246         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
247         if (p == NULL)
248                 return NULL;
249         p_fc = (struct pipeline_flow_classification *) p;
250
251         strcpy(p->name, params->name);
252         p->log_level = params->log_level;
253
254         PLOG(p, HIGH, "Flow classification");
255
256         /* Parse arguments */
257         if (pipeline_fc_parse_args(p_fc, params))
258                 return NULL;
259
260         /* Pipeline */
261         {
262                 struct rte_pipeline_params pipeline_params = {
263                         .name = params->name,
264                         .socket_id = params->socket_id,
265                         .offset_port_id = 0,
266                 };
267
268                 p->p = rte_pipeline_create(&pipeline_params);
269                 if (p->p == NULL) {
270                         rte_free(p);
271                         return NULL;
272                 }
273         }
274
275         /* Input ports */
276         p->n_ports_in = params->n_ports_in;
277         for (i = 0; i < p->n_ports_in; i++) {
278                 struct rte_pipeline_port_in_params port_params = {
279                         .ops = pipeline_port_in_params_get_ops(
280                                 &params->port_in[i]),
281                         .arg_create = pipeline_port_in_params_convert(
282                                 &params->port_in[i]),
283                         .f_action = NULL,
284                         .arg_ah = NULL,
285                         .burst_size = params->port_in[i].burst_size,
286                 };
287
288                 int status = rte_pipeline_port_in_create(p->p,
289                         &port_params,
290                         &p->port_in_id[i]);
291
292                 if (status) {
293                         rte_pipeline_free(p->p);
294                         rte_free(p);
295                         return NULL;
296                 }
297         }
298
299         /* Output ports */
300         p->n_ports_out = params->n_ports_out;
301         for (i = 0; i < p->n_ports_out; i++) {
302                 struct rte_pipeline_port_out_params port_params = {
303                         .ops = pipeline_port_out_params_get_ops(
304                                 &params->port_out[i]),
305                         .arg_create = pipeline_port_out_params_convert(
306                                 &params->port_out[i]),
307                         .f_action = NULL,
308                         .f_action_bulk = NULL,
309                         .arg_ah = NULL,
310                 };
311
312                 int status = rte_pipeline_port_out_create(p->p,
313                         &port_params,
314                         &p->port_out_id[i]);
315
316                 if (status) {
317                         rte_pipeline_free(p->p);
318                         rte_free(p);
319                         return NULL;
320                 }
321         }
322
323         /* Tables */
324         p->n_tables = 1;
325         {
326                 struct rte_table_hash_key8_ext_params
327                         table_hash_key8_params = {
328                         .n_entries = p_fc->n_flows,
329                         .n_entries_ext = p_fc->n_flows,
330                         .signature_offset = p_fc->hash_offset,
331                         .key_offset = p_fc->key_offset,
332                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
333                         .key_mask = p_fc->key_mask,
334                         .seed = 0,
335                 };
336
337                 struct rte_table_hash_key16_ext_params
338                         table_hash_key16_params = {
339                         .n_entries = p_fc->n_flows,
340                         .n_entries_ext = p_fc->n_flows,
341                         .signature_offset = p_fc->hash_offset,
342                         .key_offset = p_fc->key_offset,
343                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
344                         .key_mask = p_fc->key_mask,
345                         .seed = 0,
346                 };
347
348                 struct rte_table_hash_ext_params
349                         table_hash_params = {
350                         .key_size = p_fc->key_size,
351                         .n_keys = p_fc->n_flows,
352                         .n_buckets = p_fc->n_flows / 4,
353                         .n_buckets_ext = p_fc->n_flows / 4,
354                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
355                         .seed = 0,
356                         .signature_offset = p_fc->hash_offset,
357                         .key_offset = p_fc->key_offset,
358                 };
359
360                 struct rte_pipeline_table_params table_params = {
361                         .ops = NULL, /* set below */
362                         .arg_create = NULL, /* set below */
363                         .f_action_hit = NULL,
364                         .f_action_miss = NULL,
365                         .arg_ah = NULL,
366                         .action_data_size = sizeof(struct flow_table_entry) -
367                                 sizeof(struct rte_pipeline_table_entry),
368                 };
369
370                 int status;
371
372                 switch (p_fc->key_size) {
373                 case 8:
374                         if (p_fc->hash_offset != 0) {
375                                 table_params.ops =
376                                         &rte_table_hash_key8_ext_ops;
377                         } else {
378                                 table_params.ops =
379                                         &rte_table_hash_key8_ext_dosig_ops;
380                         }
381                         table_params.arg_create = &table_hash_key8_params;
382                         break;
383                         break;
384
385                 case 16:
386                         if (p_fc->hash_offset != 0) {
387                                 table_params.ops =
388                                         &rte_table_hash_key16_ext_ops;
389                         } else {
390                                 table_params.ops =
391                                         &rte_table_hash_key16_ext_dosig_ops;
392                         }
393                         table_params.arg_create = &table_hash_key16_params;
394                         break;
395
396                 default:
397                         table_params.ops = &rte_table_hash_ext_ops;
398                         table_params.arg_create = &table_hash_params;
399                 }
400
401                 status = rte_pipeline_table_create(p->p,
402                         &table_params,
403                         &p->table_id[0]);
404
405                 if (status) {
406                         rte_pipeline_free(p->p);
407                         rte_free(p);
408                         return NULL;
409                 }
410         }
411
412         /* Connecting input ports to tables */
413         for (i = 0; i < p->n_ports_in; i++) {
414                 int status = rte_pipeline_port_in_connect_to_table(p->p,
415                         p->port_in_id[i],
416                         p->table_id[0]);
417
418                 if (status) {
419                         rte_pipeline_free(p->p);
420                         rte_free(p);
421                         return NULL;
422                 }
423         }
424
425         /* Enable input ports */
426         for (i = 0; i < p->n_ports_in; i++) {
427                 int status = rte_pipeline_port_in_enable(p->p,
428                         p->port_in_id[i]);
429
430                 if (status) {
431                         rte_pipeline_free(p->p);
432                         rte_free(p);
433                         return NULL;
434                 }
435         }
436
437         /* Check pipeline consistency */
438         if (rte_pipeline_check(p->p) < 0) {
439                 rte_pipeline_free(p->p);
440                 rte_free(p);
441                 return NULL;
442         }
443
444         /* Message queues */
445         p->n_msgq = params->n_msgq;
446         for (i = 0; i < p->n_msgq; i++)
447                 p->msgq_in[i] = params->msgq_in[i];
448         for (i = 0; i < p->n_msgq; i++)
449                 p->msgq_out[i] = params->msgq_out[i];
450
451         /* Message handlers */
452         memcpy(p->handlers, handlers, sizeof(p->handlers));
453         memcpy(p_fc->custom_handlers,
454                 custom_handlers,
455                 sizeof(p_fc->custom_handlers));
456
457         return p;
458 }
459
460 static int
461 pipeline_fc_free(void *pipeline)
462 {
463         struct pipeline *p = (struct pipeline *) pipeline;
464
465         /* Check input arguments */
466         if (p == NULL)
467                 return -1;
468
469         /* Free resources */
470         rte_pipeline_free(p->p);
471         rte_free(p);
472         return 0;
473 }
474
475 static int
476 pipeline_fc_track(void *pipeline,
477         __rte_unused uint32_t port_in,
478         uint32_t *port_out)
479 {
480         struct pipeline *p = (struct pipeline *) pipeline;
481
482         /* Check input arguments */
483         if ((p == NULL) ||
484                 (port_in >= p->n_ports_in) ||
485                 (port_out == NULL))
486                 return -1;
487
488         if (p->n_ports_in == 1) {
489                 *port_out = 0;
490                 return 0;
491         }
492
493         return -1;
494 }
495
496 static int
497 pipeline_fc_timer(void *pipeline)
498 {
499         struct pipeline *p = (struct pipeline *) pipeline;
500
501         pipeline_msg_req_handle(p);
502         rte_pipeline_flush(p->p);
503
504         return 0;
505 }
506
507 static void *
508 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
509 {
510         struct pipeline_flow_classification *p_fc =
511                         (struct pipeline_flow_classification *) p;
512         struct pipeline_custom_msg_req *req = msg;
513         pipeline_msg_req_handler f_handle;
514
515         f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
516                 p_fc->custom_handlers[req->subtype] :
517                 pipeline_msg_req_invalid_handler;
518
519         if (f_handle == NULL)
520                 f_handle = pipeline_msg_req_invalid_handler;
521
522         return f_handle(p, req);
523 }
524
525 static void *
526 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
527 {
528         struct pipeline_fc_add_msg_req *req = msg;
529         struct pipeline_fc_add_msg_rsp *rsp = msg;
530
531         struct flow_table_entry entry = {
532                 .head = {
533                         .action = RTE_PIPELINE_ACTION_PORT,
534                         {.port_id = p->port_out_id[req->port_id]},
535                 },
536         };
537
538         rsp->status = rte_pipeline_table_entry_add(p->p,
539                 p->table_id[0],
540                 &req->key,
541                 (struct rte_pipeline_table_entry *) &entry,
542                 &rsp->key_found,
543                 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
544
545         return rsp;
546 }
547
548 static void *
549 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
550 {
551         struct pipeline_fc_add_bulk_msg_req *req = msg;
552         struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
553         uint32_t i;
554
555         for (i = 0; i < req->n_keys; i++) {
556                 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
557                 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
558
559                 struct flow_table_entry entry = {
560                         .head = {
561                                 .action = RTE_PIPELINE_ACTION_PORT,
562                                 {.port_id = p->port_out_id[flow_req->port_id]},
563                         },
564                 };
565
566                 int status = rte_pipeline_table_entry_add(p->p,
567                         p->table_id[0],
568                         &flow_req->key,
569                         (struct rte_pipeline_table_entry *) &entry,
570                         &flow_rsp->key_found,
571                         (struct rte_pipeline_table_entry **)
572                                 &flow_rsp->entry_ptr);
573
574                 if (status)
575                         break;
576         }
577
578         rsp->n_keys = i;
579
580         return rsp;
581 }
582
583 static void *
584 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
585 {
586         struct pipeline_fc_del_msg_req *req = msg;
587         struct pipeline_fc_del_msg_rsp *rsp = msg;
588
589         rsp->status = rte_pipeline_table_entry_delete(p->p,
590                 p->table_id[0],
591                 &req->key,
592                 &rsp->key_found,
593                 NULL);
594
595         return rsp;
596 }
597
598 static void *
599 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
600 {
601         struct pipeline_fc_add_default_msg_req *req = msg;
602         struct pipeline_fc_add_default_msg_rsp *rsp = msg;
603
604         struct flow_table_entry default_entry = {
605                 .head = {
606                         .action = RTE_PIPELINE_ACTION_PORT,
607                         {.port_id = p->port_out_id[req->port_id]},
608                 },
609         };
610
611         rsp->status = rte_pipeline_table_default_entry_add(p->p,
612                 p->table_id[0],
613                 (struct rte_pipeline_table_entry *) &default_entry,
614                 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
615
616         return rsp;
617 }
618
619 static void *
620 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
621 {
622         struct pipeline_fc_del_default_msg_rsp *rsp = msg;
623
624         rsp->status = rte_pipeline_table_default_entry_delete(p->p,
625                 p->table_id[0],
626                 NULL);
627
628         return rsp;
629 }
630
631 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
632         .f_init = pipeline_fc_init,
633         .f_free = pipeline_fc_free,
634         .f_run = NULL,
635         .f_timer = pipeline_fc_timer,
636         .f_track = pipeline_fc_track,
637 };