net/softnic: add connection agent
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_internals.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
6 #define __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
7
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <sys/queue.h>
11
12 #include <rte_mempool.h>
13 #include <rte_mbuf.h>
14 #include <rte_ring.h>
15 #include <rte_ethdev.h>
16 #include <rte_sched.h>
17 #include <rte_port_in_action.h>
18 #include <rte_table_action.h>
19 #include <rte_pipeline.h>
20
21 #include <rte_ethdev_driver.h>
22 #include <rte_tm_driver.h>
23
24 #include "rte_eth_softnic.h"
25 #include "conn.h"
26
27 #define NAME_SIZE                                            64
28
29 /**
30  * PMD Parameters
31  */
32
33 struct pmd_params {
34         const char *name;
35         const char *firmware;
36         uint16_t conn_port;
37         uint32_t cpu_id;
38
39         /** Traffic Management (TM) */
40         struct {
41                 uint32_t n_queues; /**< Number of queues */
42                 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
43         } tm;
44 };
45
46 /**
47  * MEMPOOL
48  */
49 struct softnic_mempool_params {
50         uint32_t buffer_size;
51         uint32_t pool_size;
52         uint32_t cache_size;
53 };
54
55 struct softnic_mempool {
56         TAILQ_ENTRY(softnic_mempool) node;
57         char name[NAME_SIZE];
58         struct rte_mempool *m;
59         uint32_t buffer_size;
60 };
61
62 TAILQ_HEAD(softnic_mempool_list, softnic_mempool);
63
64 /**
65  * SWQ
66  */
67 struct softnic_swq_params {
68         uint32_t size;
69 };
70
71 struct softnic_swq {
72         TAILQ_ENTRY(softnic_swq) node;
73         char name[NAME_SIZE];
74         struct rte_ring *r;
75 };
76
77 TAILQ_HEAD(softnic_swq_list, softnic_swq);
78
79 /**
80  * LINK
81  */
82 struct softnic_link_params {
83         const char *dev_name;
84         uint16_t port_id; /**< Valid only when *dev_name* is NULL. */
85 };
86
87 struct softnic_link {
88         TAILQ_ENTRY(softnic_link) node;
89         char name[NAME_SIZE];
90         uint16_t port_id;
91         uint32_t n_rxq;
92         uint32_t n_txq;
93 };
94
95 TAILQ_HEAD(softnic_link_list, softnic_link);
96
97 /**
98  * TMGR
99  */
100
101 #ifndef TM_MAX_SUBPORTS
102 #define TM_MAX_SUBPORTS                                 8
103 #endif
104
105 #ifndef TM_MAX_PIPES_PER_SUBPORT
106 #define TM_MAX_PIPES_PER_SUBPORT                        4096
107 #endif
108
109 struct tm_params {
110         struct rte_sched_port_params port_params;
111
112         struct rte_sched_subport_params subport_params[TM_MAX_SUBPORTS];
113
114         struct rte_sched_pipe_params
115                 pipe_profiles[RTE_SCHED_PIPE_PROFILES_PER_PORT];
116         uint32_t n_pipe_profiles;
117         uint32_t pipe_to_profile[TM_MAX_SUBPORTS * TM_MAX_PIPES_PER_SUBPORT];
118 };
119
120 /* TM Levels */
121 enum tm_node_level {
122         TM_NODE_LEVEL_PORT = 0,
123         TM_NODE_LEVEL_SUBPORT,
124         TM_NODE_LEVEL_PIPE,
125         TM_NODE_LEVEL_TC,
126         TM_NODE_LEVEL_QUEUE,
127         TM_NODE_LEVEL_MAX,
128 };
129
130 /* TM Shaper Profile */
131 struct tm_shaper_profile {
132         TAILQ_ENTRY(tm_shaper_profile) node;
133         uint32_t shaper_profile_id;
134         uint32_t n_users;
135         struct rte_tm_shaper_params params;
136 };
137
138 TAILQ_HEAD(tm_shaper_profile_list, tm_shaper_profile);
139
140 /* TM Shared Shaper */
141 struct tm_shared_shaper {
142         TAILQ_ENTRY(tm_shared_shaper) node;
143         uint32_t shared_shaper_id;
144         uint32_t n_users;
145         uint32_t shaper_profile_id;
146 };
147
148 TAILQ_HEAD(tm_shared_shaper_list, tm_shared_shaper);
149
150 /* TM WRED Profile */
151 struct tm_wred_profile {
152         TAILQ_ENTRY(tm_wred_profile) node;
153         uint32_t wred_profile_id;
154         uint32_t n_users;
155         struct rte_tm_wred_params params;
156 };
157
158 TAILQ_HEAD(tm_wred_profile_list, tm_wred_profile);
159
160 /* TM Node */
161 struct tm_node {
162         TAILQ_ENTRY(tm_node) node;
163         uint32_t node_id;
164         uint32_t parent_node_id;
165         uint32_t priority;
166         uint32_t weight;
167         uint32_t level;
168         struct tm_node *parent_node;
169         struct tm_shaper_profile *shaper_profile;
170         struct tm_wred_profile *wred_profile;
171         struct rte_tm_node_params params;
172         struct rte_tm_node_stats stats;
173         uint32_t n_children;
174 };
175
176 TAILQ_HEAD(tm_node_list, tm_node);
177
178 /* TM Hierarchy Specification */
179 struct tm_hierarchy {
180         struct tm_shaper_profile_list shaper_profiles;
181         struct tm_shared_shaper_list shared_shapers;
182         struct tm_wred_profile_list wred_profiles;
183         struct tm_node_list nodes;
184
185         uint32_t n_shaper_profiles;
186         uint32_t n_shared_shapers;
187         uint32_t n_wred_profiles;
188         uint32_t n_nodes;
189
190         uint32_t n_tm_nodes[TM_NODE_LEVEL_MAX];
191 };
192
193 struct tm_internals {
194         /** Hierarchy specification
195          *
196          *     -Hierarchy is unfrozen at init and when port is stopped.
197          *     -Hierarchy is frozen on successful hierarchy commit.
198          *     -Run-time hierarchy changes are not allowed, therefore it makes
199          *      sense to keep the hierarchy frozen after the port is started.
200          */
201         struct tm_hierarchy h;
202         int hierarchy_frozen;
203
204         /** Blueprints */
205         struct tm_params params;
206         struct rte_sched_port *sched;
207 };
208
209 struct softnic_tmgr_port {
210         TAILQ_ENTRY(softnic_tmgr_port) node;
211         char name[NAME_SIZE];
212         struct rte_sched_port *s;
213 };
214
215 TAILQ_HEAD(softnic_tmgr_port_list, softnic_tmgr_port);
216
217 /**
218  * TAP
219  */
220 struct softnic_tap {
221         TAILQ_ENTRY(softnic_tap) node;
222         char name[NAME_SIZE];
223         int fd;
224 };
225
226 TAILQ_HEAD(softnic_tap_list, softnic_tap);
227
228 /**
229  * Input port action
230  */
231 struct softnic_port_in_action_profile_params {
232         uint64_t action_mask;
233         struct rte_port_in_action_fltr_config fltr;
234         struct rte_port_in_action_lb_config lb;
235 };
236
237 struct softnic_port_in_action_profile {
238         TAILQ_ENTRY(softnic_port_in_action_profile) node;
239         char name[NAME_SIZE];
240         struct softnic_port_in_action_profile_params params;
241         struct rte_port_in_action_profile *ap;
242 };
243
244 TAILQ_HEAD(softnic_port_in_action_profile_list, softnic_port_in_action_profile);
245
246 /**
247  * Table action
248  */
249 struct softnic_table_action_profile_params {
250         uint64_t action_mask;
251         struct rte_table_action_common_config common;
252         struct rte_table_action_lb_config lb;
253         struct rte_table_action_mtr_config mtr;
254         struct rte_table_action_tm_config tm;
255         struct rte_table_action_encap_config encap;
256         struct rte_table_action_nat_config nat;
257         struct rte_table_action_ttl_config ttl;
258         struct rte_table_action_stats_config stats;
259 };
260
261 struct softnic_table_action_profile {
262         TAILQ_ENTRY(softnic_table_action_profile) node;
263         char name[NAME_SIZE];
264         struct softnic_table_action_profile_params params;
265         struct rte_table_action_profile *ap;
266 };
267
268 TAILQ_HEAD(softnic_table_action_profile_list, softnic_table_action_profile);
269
270 /**
271  * Pipeline
272  */
273 struct pipeline_params {
274         uint32_t timer_period_ms;
275         uint32_t offset_port_id;
276 };
277
278 enum softnic_port_in_type {
279         PORT_IN_RXQ,
280         PORT_IN_SWQ,
281         PORT_IN_TMGR,
282         PORT_IN_TAP,
283         PORT_IN_SOURCE,
284 };
285
286 struct softnic_port_in_params {
287         /* Read */
288         enum softnic_port_in_type type;
289         const char *dev_name;
290         union {
291                 struct {
292                         uint16_t queue_id;
293                 } rxq;
294
295                 struct {
296                         const char *mempool_name;
297                         uint32_t mtu;
298                 } tap;
299
300                 struct {
301                         const char *mempool_name;
302                         const char *file_name;
303                         uint32_t n_bytes_per_pkt;
304                 } source;
305         };
306         uint32_t burst_size;
307
308         /* Action */
309         const char *action_profile_name;
310 };
311
312 enum softnic_port_out_type {
313         PORT_OUT_TXQ,
314         PORT_OUT_SWQ,
315         PORT_OUT_TMGR,
316         PORT_OUT_TAP,
317         PORT_OUT_SINK,
318 };
319
320 struct softnic_port_out_params {
321         enum softnic_port_out_type type;
322         const char *dev_name;
323         union {
324                 struct {
325                         uint16_t queue_id;
326                 } txq;
327
328                 struct {
329                         const char *file_name;
330                         uint32_t max_n_pkts;
331                 } sink;
332         };
333         uint32_t burst_size;
334         int retry;
335         uint32_t n_retries;
336 };
337
338 enum softnic_table_type {
339         TABLE_ACL,
340         TABLE_ARRAY,
341         TABLE_HASH,
342         TABLE_LPM,
343         TABLE_STUB,
344 };
345
346 struct softnic_table_acl_params {
347         uint32_t n_rules;
348         uint32_t ip_header_offset;
349         int ip_version;
350 };
351
352 struct softnic_table_array_params {
353         uint32_t n_keys;
354         uint32_t key_offset;
355 };
356
357 struct softnic_table_hash_params {
358         uint32_t n_keys;
359         uint32_t key_offset;
360         uint32_t key_size;
361         uint8_t *key_mask;
362         uint32_t n_buckets;
363         int extendable_bucket;
364 };
365
366 struct softnic_table_lpm_params {
367         uint32_t n_rules;
368         uint32_t key_offset;
369         uint32_t key_size;
370 };
371
372 struct softnic_table_params {
373         /* Match */
374         enum softnic_table_type match_type;
375         union {
376                 struct softnic_table_acl_params acl;
377                 struct softnic_table_array_params array;
378                 struct softnic_table_hash_params hash;
379                 struct softnic_table_lpm_params lpm;
380         } match;
381
382         /* Action */
383         const char *action_profile_name;
384 };
385
386 struct softnic_port_in {
387         struct softnic_port_in_params params;
388         struct softnic_port_in_action_profile *ap;
389         struct rte_port_in_action *a;
390 };
391
392 struct softnic_table {
393         struct softnic_table_params params;
394         struct softnic_table_action_profile *ap;
395         struct rte_table_action *a;
396 };
397
398 struct pipeline {
399         TAILQ_ENTRY(pipeline) node;
400         char name[NAME_SIZE];
401
402         struct rte_pipeline *p;
403         struct softnic_port_in port_in[RTE_PIPELINE_PORT_IN_MAX];
404         struct softnic_table table[RTE_PIPELINE_TABLE_MAX];
405         uint32_t n_ports_in;
406         uint32_t n_ports_out;
407         uint32_t n_tables;
408
409         struct rte_ring *msgq_req;
410         struct rte_ring *msgq_rsp;
411         uint32_t timer_period_ms;
412
413         int enabled;
414         uint32_t thread_id;
415         uint32_t cpu_id;
416 };
417
418 TAILQ_HEAD(pipeline_list, pipeline);
419
420 /**
421  * Thread
422  */
423 #ifndef THREAD_PIPELINES_MAX
424 #define THREAD_PIPELINES_MAX                               256
425 #endif
426
427 #ifndef THREAD_MSGQ_SIZE
428 #define THREAD_MSGQ_SIZE                                   64
429 #endif
430
431 #ifndef THREAD_TIMER_PERIOD_MS
432 #define THREAD_TIMER_PERIOD_MS                             100
433 #endif
434
435 /**
436  * Master thead: data plane thread context
437  */
438 struct softnic_thread {
439         struct rte_ring *msgq_req;
440         struct rte_ring *msgq_rsp;
441
442         uint32_t enabled;
443 };
444
445 /**
446  * Data plane threads: context
447  */
448 #ifndef TABLE_RULE_ACTION_SIZE_MAX
449 #define TABLE_RULE_ACTION_SIZE_MAX                         2048
450 #endif
451
452 struct softnic_table_data {
453         struct rte_table_action *a;
454 };
455
456 struct pipeline_data {
457         struct rte_pipeline *p;
458         struct softnic_table_data table_data[RTE_PIPELINE_TABLE_MAX];
459         uint32_t n_tables;
460
461         struct rte_ring *msgq_req;
462         struct rte_ring *msgq_rsp;
463         uint64_t timer_period; /* Measured in CPU cycles. */
464         uint64_t time_next;
465
466         uint8_t buffer[TABLE_RULE_ACTION_SIZE_MAX];
467 };
468
469 struct softnic_thread_data {
470         struct rte_pipeline *p[THREAD_PIPELINES_MAX];
471         uint32_t n_pipelines;
472
473         struct pipeline_data pipeline_data[THREAD_PIPELINES_MAX];
474         struct rte_ring *msgq_req;
475         struct rte_ring *msgq_rsp;
476         uint64_t timer_period; /* Measured in CPU cycles. */
477         uint64_t time_next;
478         uint64_t time_next_min;
479         uint64_t iter;
480 } __rte_cache_aligned;
481
482 /**
483  * PMD Internals
484  */
485 struct pmd_internals {
486         /** Params */
487         struct pmd_params params;
488
489         struct {
490                 struct tm_internals tm; /**< Traffic Management */
491         } soft;
492
493         struct softnic_conn *conn;
494         struct softnic_mempool_list mempool_list;
495         struct softnic_swq_list swq_list;
496         struct softnic_link_list link_list;
497         struct softnic_tmgr_port_list tmgr_port_list;
498         struct softnic_tap_list tap_list;
499         struct softnic_port_in_action_profile_list port_in_action_profile_list;
500         struct softnic_table_action_profile_list table_action_profile_list;
501         struct pipeline_list pipeline_list;
502         struct softnic_thread thread[RTE_MAX_LCORE];
503         struct softnic_thread_data thread_data[RTE_MAX_LCORE];
504 };
505
506 /**
507  * MEMPOOL
508  */
509 int
510 softnic_mempool_init(struct pmd_internals *p);
511
512 void
513 softnic_mempool_free(struct pmd_internals *p);
514
515 struct softnic_mempool *
516 softnic_mempool_find(struct pmd_internals *p,
517         const char *name);
518
519 struct softnic_mempool *
520 softnic_mempool_create(struct pmd_internals *p,
521         const char *name,
522         struct softnic_mempool_params *params);
523
524 /**
525  * SWQ
526  */
527 int
528 softnic_swq_init(struct pmd_internals *p);
529
530 void
531 softnic_swq_free(struct pmd_internals *p);
532
533 struct softnic_swq *
534 softnic_swq_find(struct pmd_internals *p,
535         const char *name);
536
537 struct softnic_swq *
538 softnic_swq_create(struct pmd_internals *p,
539         const char *name,
540         struct softnic_swq_params *params);
541
542 /**
543  * LINK
544  */
545 int
546 softnic_link_init(struct pmd_internals *p);
547
548 void
549 softnic_link_free(struct pmd_internals *p);
550
551 struct softnic_link *
552 softnic_link_find(struct pmd_internals *p,
553         const char *name);
554
555 struct softnic_link *
556 softnic_link_create(struct pmd_internals *p,
557         const char *name,
558         struct softnic_link_params *params);
559
560 /**
561  * TMGR
562  */
563 int
564 softnic_tmgr_init(struct pmd_internals *p);
565
566 void
567 softnic_tmgr_free(struct pmd_internals *p);
568
569 struct softnic_tmgr_port *
570 softnic_tmgr_port_find(struct pmd_internals *p,
571         const char *name);
572
573 struct softnic_tmgr_port *
574 softnic_tmgr_port_create(struct pmd_internals *p,
575         const char *name,
576         struct rte_sched_port *sched);
577
578 int
579 tm_init(struct pmd_internals *p);
580
581 void
582 tm_free(struct pmd_internals *p);
583
584 int
585 tm_start(struct pmd_internals *p);
586
587 void
588 tm_stop(struct pmd_internals *p);
589
590 static inline int
591 tm_used(struct rte_eth_dev *dev)
592 {
593         struct pmd_internals *p = dev->data->dev_private;
594
595         return p->soft.tm.h.n_tm_nodes[TM_NODE_LEVEL_PORT];
596 }
597
598 extern const struct rte_tm_ops pmd_tm_ops;
599
600 /**
601  * TAP
602  */
603 int
604 softnic_tap_init(struct pmd_internals *p);
605
606 void
607 softnic_tap_free(struct pmd_internals *p);
608
609 struct softnic_tap *
610 softnic_tap_find(struct pmd_internals *p,
611         const char *name);
612
613 struct softnic_tap *
614 softnic_tap_create(struct pmd_internals *p,
615         const char *name);
616
617 /**
618  * Input port action
619  */
620 int
621 softnic_port_in_action_profile_init(struct pmd_internals *p);
622
623 void
624 softnic_port_in_action_profile_free(struct pmd_internals *p);
625
626 struct softnic_port_in_action_profile *
627 softnic_port_in_action_profile_find(struct pmd_internals *p,
628         const char *name);
629
630 struct softnic_port_in_action_profile *
631 softnic_port_in_action_profile_create(struct pmd_internals *p,
632         const char *name,
633         struct softnic_port_in_action_profile_params *params);
634
635 /**
636  * Table action
637  */
638 int
639 softnic_table_action_profile_init(struct pmd_internals *p);
640
641 void
642 softnic_table_action_profile_free(struct pmd_internals *p);
643
644 struct softnic_table_action_profile *
645 softnic_table_action_profile_find(struct pmd_internals *p,
646         const char *name);
647
648 struct softnic_table_action_profile *
649 softnic_table_action_profile_create(struct pmd_internals *p,
650         const char *name,
651         struct softnic_table_action_profile_params *params);
652
653 /**
654  * Pipeline
655  */
656 int
657 softnic_pipeline_init(struct pmd_internals *p);
658
659 void
660 softnic_pipeline_free(struct pmd_internals *p);
661
662 struct pipeline *
663 softnic_pipeline_find(struct pmd_internals *p, const char *name);
664
665 struct pipeline *
666 softnic_pipeline_create(struct pmd_internals *p,
667         const char *name,
668         struct pipeline_params *params);
669
670 int
671 softnic_pipeline_port_in_create(struct pmd_internals *p,
672         const char *pipeline_name,
673         struct softnic_port_in_params *params,
674         int enabled);
675
676 int
677 softnic_pipeline_port_in_connect_to_table(struct pmd_internals *p,
678         const char *pipeline_name,
679         uint32_t port_id,
680         uint32_t table_id);
681
682 int
683 softnic_pipeline_port_out_create(struct pmd_internals *p,
684         const char *pipeline_name,
685         struct softnic_port_out_params *params);
686
687 int
688 softnic_pipeline_table_create(struct pmd_internals *p,
689         const char *pipeline_name,
690         struct softnic_table_params *params);
691
692 /**
693  * Thread
694  */
695 int
696 softnic_thread_init(struct pmd_internals *p);
697
698 void
699 softnic_thread_free(struct pmd_internals *p);
700
701 /**
702  * CLI
703  */
704 void
705 softnic_cli_process(char *in,
706         char *out,
707         size_t out_size,
708         void *arg);
709
710 int
711 softnic_cli_script_process(struct pmd_internals *softnic,
712         const char *file_name,
713         size_t msg_in_len_max,
714         size_t msg_out_len_max);
715
716 #endif /* __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__ */