New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_pipeline / rte_table_action.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <rte_common.h>
8 #include <rte_byteorder.h>
9 #include <rte_cycles.h>
10 #include <rte_malloc.h>
11 #include <rte_memcpy.h>
12 #include <rte_ether.h>
13 #include <rte_ip.h>
14 #include <rte_esp.h>
15 #include <rte_tcp.h>
16 #include <rte_udp.h>
17 #include <rte_cryptodev.h>
18 #include <rte_cryptodev_pmd.h>
19
20 #include "rte_table_action.h"
21
22 #define rte_htons rte_cpu_to_be_16
23 #define rte_htonl rte_cpu_to_be_32
24
25 #define rte_ntohs rte_be_to_cpu_16
26 #define rte_ntohl rte_be_to_cpu_32
27
28 /**
29  * RTE_TABLE_ACTION_FWD
30  */
31 #define fwd_data rte_pipeline_table_entry
32
33 static int
34 fwd_apply(struct fwd_data *data,
35         struct rte_table_action_fwd_params *p)
36 {
37         data->action = p->action;
38
39         if (p->action == RTE_PIPELINE_ACTION_PORT)
40                 data->port_id = p->id;
41
42         if (p->action == RTE_PIPELINE_ACTION_TABLE)
43                 data->table_id = p->id;
44
45         return 0;
46 }
47
48 /**
49  * RTE_TABLE_ACTION_LB
50  */
51 static int
52 lb_cfg_check(struct rte_table_action_lb_config *cfg)
53 {
54         if ((cfg == NULL) ||
55                 (cfg->key_size < RTE_TABLE_ACTION_LB_KEY_SIZE_MIN) ||
56                 (cfg->key_size > RTE_TABLE_ACTION_LB_KEY_SIZE_MAX) ||
57                 (!rte_is_power_of_2(cfg->key_size)) ||
58                 (cfg->f_hash == NULL))
59                 return -1;
60
61         return 0;
62 }
63
64 struct lb_data {
65         uint32_t out[RTE_TABLE_ACTION_LB_TABLE_SIZE];
66 } __attribute__((__packed__));
67
68 static int
69 lb_apply(struct lb_data *data,
70         struct rte_table_action_lb_params *p)
71 {
72         memcpy(data->out, p->out, sizeof(data->out));
73
74         return 0;
75 }
76
77 static __rte_always_inline void
78 pkt_work_lb(struct rte_mbuf *mbuf,
79         struct lb_data *data,
80         struct rte_table_action_lb_config *cfg)
81 {
82         uint8_t *pkt_key = RTE_MBUF_METADATA_UINT8_PTR(mbuf, cfg->key_offset);
83         uint32_t *out = RTE_MBUF_METADATA_UINT32_PTR(mbuf, cfg->out_offset);
84         uint64_t digest, pos;
85         uint32_t out_val;
86
87         digest = cfg->f_hash(pkt_key,
88                 cfg->key_mask,
89                 cfg->key_size,
90                 cfg->seed);
91         pos = digest & (RTE_TABLE_ACTION_LB_TABLE_SIZE - 1);
92         out_val = data->out[pos];
93
94         *out = out_val;
95 }
96
97 /**
98  * RTE_TABLE_ACTION_MTR
99  */
100 static int
101 mtr_cfg_check(struct rte_table_action_mtr_config *mtr)
102 {
103         if ((mtr->alg == RTE_TABLE_ACTION_METER_SRTCM) ||
104                 ((mtr->n_tc != 1) && (mtr->n_tc != 4)) ||
105                 (mtr->n_bytes_enabled != 0))
106                 return -ENOTSUP;
107         return 0;
108 }
109
110 #define MBUF_SCHED_QUEUE_TC_COLOR(queue, tc, color)        \
111         ((uint16_t)((((uint64_t)(queue)) & 0x3) |          \
112         ((((uint64_t)(tc)) & 0x3) << 2) |                  \
113         ((((uint64_t)(color)) & 0x3) << 4)))
114
115 #define MBUF_SCHED_COLOR(sched, color)                     \
116         (((sched) & (~0x30LLU)) | ((color) << 4))
117
118 struct mtr_trtcm_data {
119         struct rte_meter_trtcm trtcm;
120         uint64_t stats[e_RTE_METER_COLORS];
121 } __attribute__((__packed__));
122
123 #define MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data)          \
124         (((data)->stats[e_RTE_METER_GREEN] & 0xF8LLU) >> 3)
125
126 static void
127 mtr_trtcm_data_meter_profile_id_set(struct mtr_trtcm_data *data,
128         uint32_t profile_id)
129 {
130         data->stats[e_RTE_METER_GREEN] &= ~0xF8LLU;
131         data->stats[e_RTE_METER_GREEN] |= (profile_id % 32) << 3;
132 }
133
134 #define MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color)\
135         (((data)->stats[(color)] & 4LLU) >> 2)
136
137 #define MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color)\
138         ((enum rte_meter_color)((data)->stats[(color)] & 3LLU))
139
140 static void
141 mtr_trtcm_data_policer_action_set(struct mtr_trtcm_data *data,
142         enum rte_meter_color color,
143         enum rte_table_action_policer action)
144 {
145         if (action == RTE_TABLE_ACTION_POLICER_DROP) {
146                 data->stats[color] |= 4LLU;
147         } else {
148                 data->stats[color] &= ~7LLU;
149                 data->stats[color] |= color & 3LLU;
150         }
151 }
152
153 static uint64_t
154 mtr_trtcm_data_stats_get(struct mtr_trtcm_data *data,
155         enum rte_meter_color color)
156 {
157         return data->stats[color] >> 8;
158 }
159
160 static void
161 mtr_trtcm_data_stats_reset(struct mtr_trtcm_data *data,
162         enum rte_meter_color color)
163 {
164         data->stats[color] &= 0xFFLU;
165 }
166
167 #define MTR_TRTCM_DATA_STATS_INC(data, color)              \
168         ((data)->stats[(color)] += (1LLU << 8))
169
170 static size_t
171 mtr_data_size(struct rte_table_action_mtr_config *mtr)
172 {
173         return mtr->n_tc * sizeof(struct mtr_trtcm_data);
174 }
175
176 struct dscp_table_entry_data {
177         enum rte_meter_color color;
178         uint16_t tc;
179         uint16_t queue_tc_color;
180 };
181
182 struct dscp_table_data {
183         struct dscp_table_entry_data entry[64];
184 };
185
186 struct meter_profile_data {
187         struct rte_meter_trtcm_profile profile;
188         uint32_t profile_id;
189         int valid;
190 };
191
192 static struct meter_profile_data *
193 meter_profile_data_find(struct meter_profile_data *mp,
194         uint32_t mp_size,
195         uint32_t profile_id)
196 {
197         uint32_t i;
198
199         for (i = 0; i < mp_size; i++) {
200                 struct meter_profile_data *mp_data = &mp[i];
201
202                 if (mp_data->valid && (mp_data->profile_id == profile_id))
203                         return mp_data;
204         }
205
206         return NULL;
207 }
208
209 static struct meter_profile_data *
210 meter_profile_data_find_unused(struct meter_profile_data *mp,
211         uint32_t mp_size)
212 {
213         uint32_t i;
214
215         for (i = 0; i < mp_size; i++) {
216                 struct meter_profile_data *mp_data = &mp[i];
217
218                 if (!mp_data->valid)
219                         return mp_data;
220         }
221
222         return NULL;
223 }
224
225 static int
226 mtr_apply_check(struct rte_table_action_mtr_params *p,
227         struct rte_table_action_mtr_config *cfg,
228         struct meter_profile_data *mp,
229         uint32_t mp_size)
230 {
231         uint32_t i;
232
233         if (p->tc_mask > RTE_LEN2MASK(cfg->n_tc, uint32_t))
234                 return -EINVAL;
235
236         for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
237                 struct rte_table_action_mtr_tc_params *p_tc = &p->mtr[i];
238                 struct meter_profile_data *mp_data;
239
240                 if ((p->tc_mask & (1LLU << i)) == 0)
241                         continue;
242
243                 mp_data = meter_profile_data_find(mp,
244                         mp_size,
245                         p_tc->meter_profile_id);
246                 if (!mp_data)
247                         return -EINVAL;
248         }
249
250         return 0;
251 }
252
253 static int
254 mtr_apply(struct mtr_trtcm_data *data,
255         struct rte_table_action_mtr_params *p,
256         struct rte_table_action_mtr_config *cfg,
257         struct meter_profile_data *mp,
258         uint32_t mp_size)
259 {
260         uint32_t i;
261         int status;
262
263         /* Check input arguments */
264         status = mtr_apply_check(p, cfg, mp, mp_size);
265         if (status)
266                 return status;
267
268         /* Apply */
269         for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
270                 struct rte_table_action_mtr_tc_params *p_tc = &p->mtr[i];
271                 struct mtr_trtcm_data *data_tc = &data[i];
272                 struct meter_profile_data *mp_data;
273
274                 if ((p->tc_mask & (1LLU << i)) == 0)
275                         continue;
276
277                 /* Find profile */
278                 mp_data = meter_profile_data_find(mp,
279                         mp_size,
280                         p_tc->meter_profile_id);
281                 if (!mp_data)
282                         return -EINVAL;
283
284                 memset(data_tc, 0, sizeof(*data_tc));
285
286                 /* Meter object */
287                 status = rte_meter_trtcm_config(&data_tc->trtcm,
288                         &mp_data->profile);
289                 if (status)
290                         return status;
291
292                 /* Meter profile */
293                 mtr_trtcm_data_meter_profile_id_set(data_tc,
294                         mp_data - mp);
295
296                 /* Policer actions */
297                 mtr_trtcm_data_policer_action_set(data_tc,
298                         e_RTE_METER_GREEN,
299                         p_tc->policer[e_RTE_METER_GREEN]);
300
301                 mtr_trtcm_data_policer_action_set(data_tc,
302                         e_RTE_METER_YELLOW,
303                         p_tc->policer[e_RTE_METER_YELLOW]);
304
305                 mtr_trtcm_data_policer_action_set(data_tc,
306                         e_RTE_METER_RED,
307                         p_tc->policer[e_RTE_METER_RED]);
308         }
309
310         return 0;
311 }
312
313 static __rte_always_inline uint64_t
314 pkt_work_mtr(struct rte_mbuf *mbuf,
315         struct mtr_trtcm_data *data,
316         struct dscp_table_data *dscp_table,
317         struct meter_profile_data *mp,
318         uint64_t time,
319         uint32_t dscp,
320         uint16_t total_length)
321 {
322         uint64_t drop_mask, sched;
323         uint64_t *sched_ptr = (uint64_t *) &mbuf->hash.sched;
324         struct dscp_table_entry_data *dscp_entry = &dscp_table->entry[dscp];
325         enum rte_meter_color color_in, color_meter, color_policer;
326         uint32_t tc, mp_id;
327
328         tc = dscp_entry->tc;
329         color_in = dscp_entry->color;
330         data += tc;
331         mp_id = MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data);
332         sched = *sched_ptr;
333
334         /* Meter */
335         color_meter = rte_meter_trtcm_color_aware_check(
336                 &data->trtcm,
337                 &mp[mp_id].profile,
338                 time,
339                 total_length,
340                 color_in);
341
342         /* Stats */
343         MTR_TRTCM_DATA_STATS_INC(data, color_meter);
344
345         /* Police */
346         drop_mask = MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color_meter);
347         color_policer =
348                 MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color_meter);
349         *sched_ptr = MBUF_SCHED_COLOR(sched, color_policer);
350
351         return drop_mask;
352 }
353
354 /**
355  * RTE_TABLE_ACTION_TM
356  */
357 static int
358 tm_cfg_check(struct rte_table_action_tm_config *tm)
359 {
360         if ((tm->n_subports_per_port == 0) ||
361                 (rte_is_power_of_2(tm->n_subports_per_port) == 0) ||
362                 (tm->n_subports_per_port > UINT16_MAX) ||
363                 (tm->n_pipes_per_subport == 0) ||
364                 (rte_is_power_of_2(tm->n_pipes_per_subport) == 0))
365                 return -ENOTSUP;
366
367         return 0;
368 }
369
370 struct tm_data {
371         uint16_t queue_tc_color;
372         uint16_t subport;
373         uint32_t pipe;
374 } __attribute__((__packed__));
375
376 static int
377 tm_apply_check(struct rte_table_action_tm_params *p,
378         struct rte_table_action_tm_config *cfg)
379 {
380         if ((p->subport_id >= cfg->n_subports_per_port) ||
381                 (p->pipe_id >= cfg->n_pipes_per_subport))
382                 return -EINVAL;
383
384         return 0;
385 }
386
387 static int
388 tm_apply(struct tm_data *data,
389         struct rte_table_action_tm_params *p,
390         struct rte_table_action_tm_config *cfg)
391 {
392         int status;
393
394         /* Check input arguments */
395         status = tm_apply_check(p, cfg);
396         if (status)
397                 return status;
398
399         /* Apply */
400         data->queue_tc_color = 0;
401         data->subport = (uint16_t) p->subport_id;
402         data->pipe = p->pipe_id;
403
404         return 0;
405 }
406
407 static __rte_always_inline void
408 pkt_work_tm(struct rte_mbuf *mbuf,
409         struct tm_data *data,
410         struct dscp_table_data *dscp_table,
411         uint32_t dscp)
412 {
413         struct dscp_table_entry_data *dscp_entry = &dscp_table->entry[dscp];
414         struct tm_data *sched_ptr = (struct tm_data *) &mbuf->hash.sched;
415         struct tm_data sched;
416
417         sched = *data;
418         sched.queue_tc_color = dscp_entry->queue_tc_color;
419         *sched_ptr = sched;
420 }
421
422 /**
423  * RTE_TABLE_ACTION_ENCAP
424  */
425 static int
426 encap_valid(enum rte_table_action_encap_type encap)
427 {
428         switch (encap) {
429         case RTE_TABLE_ACTION_ENCAP_ETHER:
430         case RTE_TABLE_ACTION_ENCAP_VLAN:
431         case RTE_TABLE_ACTION_ENCAP_QINQ:
432         case RTE_TABLE_ACTION_ENCAP_MPLS:
433         case RTE_TABLE_ACTION_ENCAP_PPPOE:
434         case RTE_TABLE_ACTION_ENCAP_VXLAN:
435                 return 1;
436         default:
437                 return 0;
438         }
439 }
440
441 static int
442 encap_cfg_check(struct rte_table_action_encap_config *encap)
443 {
444         if ((encap->encap_mask == 0) ||
445                 (__builtin_popcountll(encap->encap_mask) != 1))
446                 return -ENOTSUP;
447
448         return 0;
449 }
450
451 struct encap_ether_data {
452         struct ether_hdr ether;
453 } __attribute__((__packed__));
454
455 #define VLAN(pcp, dei, vid)                                \
456         ((uint16_t)((((uint64_t)(pcp)) & 0x7LLU) << 13) |  \
457         ((((uint64_t)(dei)) & 0x1LLU) << 12) |             \
458         (((uint64_t)(vid)) & 0xFFFLLU))                    \
459
460 struct encap_vlan_data {
461         struct ether_hdr ether;
462         struct vlan_hdr vlan;
463 } __attribute__((__packed__));
464
465 struct encap_qinq_data {
466         struct ether_hdr ether;
467         struct vlan_hdr svlan;
468         struct vlan_hdr cvlan;
469 } __attribute__((__packed__));
470
471 #define ETHER_TYPE_MPLS_UNICAST                            0x8847
472
473 #define ETHER_TYPE_MPLS_MULTICAST                          0x8848
474
475 #define MPLS(label, tc, s, ttl)                            \
476         ((uint32_t)(((((uint64_t)(label)) & 0xFFFFFLLU) << 12) |\
477         ((((uint64_t)(tc)) & 0x7LLU) << 9) |               \
478         ((((uint64_t)(s)) & 0x1LLU) << 8) |                \
479         (((uint64_t)(ttl)) & 0xFFLLU)))
480
481 struct encap_mpls_data {
482         struct ether_hdr ether;
483         uint32_t mpls[RTE_TABLE_ACTION_MPLS_LABELS_MAX];
484         uint32_t mpls_count;
485 } __attribute__((__packed__));
486
487 #define ETHER_TYPE_PPPOE_SESSION                           0x8864
488
489 #define PPP_PROTOCOL_IP                                    0x0021
490
491 struct pppoe_ppp_hdr {
492         uint16_t ver_type_code;
493         uint16_t session_id;
494         uint16_t length;
495         uint16_t protocol;
496 } __attribute__((__packed__));
497
498 struct encap_pppoe_data {
499         struct ether_hdr ether;
500         struct pppoe_ppp_hdr pppoe_ppp;
501 } __attribute__((__packed__));
502
503 #define IP_PROTO_UDP                                       17
504
505 struct encap_vxlan_ipv4_data {
506         struct ether_hdr ether;
507         struct ipv4_hdr ipv4;
508         struct udp_hdr udp;
509         struct vxlan_hdr vxlan;
510 } __attribute__((__packed__));
511
512 struct encap_vxlan_ipv4_vlan_data {
513         struct ether_hdr ether;
514         struct vlan_hdr vlan;
515         struct ipv4_hdr ipv4;
516         struct udp_hdr udp;
517         struct vxlan_hdr vxlan;
518 } __attribute__((__packed__));
519
520 struct encap_vxlan_ipv6_data {
521         struct ether_hdr ether;
522         struct ipv6_hdr ipv6;
523         struct udp_hdr udp;
524         struct vxlan_hdr vxlan;
525 } __attribute__((__packed__));
526
527 struct encap_vxlan_ipv6_vlan_data {
528         struct ether_hdr ether;
529         struct vlan_hdr vlan;
530         struct ipv6_hdr ipv6;
531         struct udp_hdr udp;
532         struct vxlan_hdr vxlan;
533 } __attribute__((__packed__));
534
535 static size_t
536 encap_data_size(struct rte_table_action_encap_config *encap)
537 {
538         switch (encap->encap_mask) {
539         case 1LLU << RTE_TABLE_ACTION_ENCAP_ETHER:
540                 return sizeof(struct encap_ether_data);
541
542         case 1LLU << RTE_TABLE_ACTION_ENCAP_VLAN:
543                 return sizeof(struct encap_vlan_data);
544
545         case 1LLU << RTE_TABLE_ACTION_ENCAP_QINQ:
546                 return sizeof(struct encap_qinq_data);
547
548         case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
549                 return sizeof(struct encap_mpls_data);
550
551         case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
552                 return sizeof(struct encap_pppoe_data);
553
554         case 1LLU << RTE_TABLE_ACTION_ENCAP_VXLAN:
555                 if (encap->vxlan.ip_version)
556                         if (encap->vxlan.vlan)
557                                 return sizeof(struct encap_vxlan_ipv4_vlan_data);
558                         else
559                                 return sizeof(struct encap_vxlan_ipv4_data);
560                 else
561                         if (encap->vxlan.vlan)
562                                 return sizeof(struct encap_vxlan_ipv6_vlan_data);
563                         else
564                                 return sizeof(struct encap_vxlan_ipv6_data);
565
566         default:
567                 return 0;
568         }
569 }
570
571 static int
572 encap_apply_check(struct rte_table_action_encap_params *p,
573         struct rte_table_action_encap_config *cfg)
574 {
575         if ((encap_valid(p->type) == 0) ||
576                 ((cfg->encap_mask & (1LLU << p->type)) == 0))
577                 return -EINVAL;
578
579         switch (p->type) {
580         case RTE_TABLE_ACTION_ENCAP_ETHER:
581                 return 0;
582
583         case RTE_TABLE_ACTION_ENCAP_VLAN:
584                 return 0;
585
586         case RTE_TABLE_ACTION_ENCAP_QINQ:
587                 return 0;
588
589         case RTE_TABLE_ACTION_ENCAP_MPLS:
590                 if ((p->mpls.mpls_count == 0) ||
591                         (p->mpls.mpls_count > RTE_TABLE_ACTION_MPLS_LABELS_MAX))
592                         return -EINVAL;
593
594                 return 0;
595
596         case RTE_TABLE_ACTION_ENCAP_PPPOE:
597                 return 0;
598
599         case RTE_TABLE_ACTION_ENCAP_VXLAN:
600                 return 0;
601
602         default:
603                 return -EINVAL;
604         }
605 }
606
607 static int
608 encap_ether_apply(void *data,
609         struct rte_table_action_encap_params *p,
610         struct rte_table_action_common_config *common_cfg)
611 {
612         struct encap_ether_data *d = data;
613         uint16_t ethertype = (common_cfg->ip_version) ?
614                 ETHER_TYPE_IPv4 :
615                 ETHER_TYPE_IPv6;
616
617         /* Ethernet */
618         ether_addr_copy(&p->ether.ether.da, &d->ether.d_addr);
619         ether_addr_copy(&p->ether.ether.sa, &d->ether.s_addr);
620         d->ether.ether_type = rte_htons(ethertype);
621
622         return 0;
623 }
624
625 static int
626 encap_vlan_apply(void *data,
627         struct rte_table_action_encap_params *p,
628         struct rte_table_action_common_config *common_cfg)
629 {
630         struct encap_vlan_data *d = data;
631         uint16_t ethertype = (common_cfg->ip_version) ?
632                 ETHER_TYPE_IPv4 :
633                 ETHER_TYPE_IPv6;
634
635         /* Ethernet */
636         ether_addr_copy(&p->vlan.ether.da, &d->ether.d_addr);
637         ether_addr_copy(&p->vlan.ether.sa, &d->ether.s_addr);
638         d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);
639
640         /* VLAN */
641         d->vlan.vlan_tci = rte_htons(VLAN(p->vlan.vlan.pcp,
642                 p->vlan.vlan.dei,
643                 p->vlan.vlan.vid));
644         d->vlan.eth_proto = rte_htons(ethertype);
645
646         return 0;
647 }
648
649 static int
650 encap_qinq_apply(void *data,
651         struct rte_table_action_encap_params *p,
652         struct rte_table_action_common_config *common_cfg)
653 {
654         struct encap_qinq_data *d = data;
655         uint16_t ethertype = (common_cfg->ip_version) ?
656                 ETHER_TYPE_IPv4 :
657                 ETHER_TYPE_IPv6;
658
659         /* Ethernet */
660         ether_addr_copy(&p->qinq.ether.da, &d->ether.d_addr);
661         ether_addr_copy(&p->qinq.ether.sa, &d->ether.s_addr);
662         d->ether.ether_type = rte_htons(ETHER_TYPE_QINQ);
663
664         /* SVLAN */
665         d->svlan.vlan_tci = rte_htons(VLAN(p->qinq.svlan.pcp,
666                 p->qinq.svlan.dei,
667                 p->qinq.svlan.vid));
668         d->svlan.eth_proto = rte_htons(ETHER_TYPE_VLAN);
669
670         /* CVLAN */
671         d->cvlan.vlan_tci = rte_htons(VLAN(p->qinq.cvlan.pcp,
672                 p->qinq.cvlan.dei,
673                 p->qinq.cvlan.vid));
674         d->cvlan.eth_proto = rte_htons(ethertype);
675
676         return 0;
677 }
678
679 static int
680 encap_mpls_apply(void *data,
681         struct rte_table_action_encap_params *p)
682 {
683         struct encap_mpls_data *d = data;
684         uint16_t ethertype = (p->mpls.unicast) ?
685                 ETHER_TYPE_MPLS_UNICAST :
686                 ETHER_TYPE_MPLS_MULTICAST;
687         uint32_t i;
688
689         /* Ethernet */
690         ether_addr_copy(&p->mpls.ether.da, &d->ether.d_addr);
691         ether_addr_copy(&p->mpls.ether.sa, &d->ether.s_addr);
692         d->ether.ether_type = rte_htons(ethertype);
693
694         /* MPLS */
695         for (i = 0; i < p->mpls.mpls_count - 1; i++)
696                 d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
697                         p->mpls.mpls[i].tc,
698                         0,
699                         p->mpls.mpls[i].ttl));
700
701         d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
702                 p->mpls.mpls[i].tc,
703                 1,
704                 p->mpls.mpls[i].ttl));
705
706         d->mpls_count = p->mpls.mpls_count;
707         return 0;
708 }
709
710 static int
711 encap_pppoe_apply(void *data,
712         struct rte_table_action_encap_params *p)
713 {
714         struct encap_pppoe_data *d = data;
715
716         /* Ethernet */
717         ether_addr_copy(&p->pppoe.ether.da, &d->ether.d_addr);
718         ether_addr_copy(&p->pppoe.ether.sa, &d->ether.s_addr);
719         d->ether.ether_type = rte_htons(ETHER_TYPE_PPPOE_SESSION);
720
721         /* PPPoE and PPP*/
722         d->pppoe_ppp.ver_type_code = rte_htons(0x1100);
723         d->pppoe_ppp.session_id = rte_htons(p->pppoe.pppoe.session_id);
724         d->pppoe_ppp.length = 0; /* not pre-computed */
725         d->pppoe_ppp.protocol = rte_htons(PPP_PROTOCOL_IP);
726
727         return 0;
728 }
729
730 static int
731 encap_vxlan_apply(void *data,
732         struct rte_table_action_encap_params *p,
733         struct rte_table_action_encap_config *cfg)
734 {
735         if ((p->vxlan.vxlan.vni > 0xFFFFFF) ||
736                 (cfg->vxlan.ip_version && (p->vxlan.ipv4.dscp > 0x3F)) ||
737                 (!cfg->vxlan.ip_version && (p->vxlan.ipv6.flow_label > 0xFFFFF)) ||
738                 (!cfg->vxlan.ip_version && (p->vxlan.ipv6.dscp > 0x3F)) ||
739                 (cfg->vxlan.vlan && (p->vxlan.vlan.vid > 0xFFF)))
740                 return -1;
741
742         if (cfg->vxlan.ip_version)
743                 if (cfg->vxlan.vlan) {
744                         struct encap_vxlan_ipv4_vlan_data *d = data;
745
746                         /* Ethernet */
747                         ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
748                         ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
749                         d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);
750
751                         /* VLAN */
752                         d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
753                                 p->vxlan.vlan.dei,
754                                 p->vxlan.vlan.vid));
755                         d->vlan.eth_proto = rte_htons(ETHER_TYPE_IPv4);
756
757                         /* IPv4*/
758                         d->ipv4.version_ihl = 0x45;
759                         d->ipv4.type_of_service = p->vxlan.ipv4.dscp << 2;
760                         d->ipv4.total_length = 0; /* not pre-computed */
761                         d->ipv4.packet_id = 0;
762                         d->ipv4.fragment_offset = 0;
763                         d->ipv4.time_to_live = p->vxlan.ipv4.ttl;
764                         d->ipv4.next_proto_id = IP_PROTO_UDP;
765                         d->ipv4.hdr_checksum = 0;
766                         d->ipv4.src_addr = rte_htonl(p->vxlan.ipv4.sa);
767                         d->ipv4.dst_addr = rte_htonl(p->vxlan.ipv4.da);
768
769                         d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);
770
771                         /* UDP */
772                         d->udp.src_port = rte_htons(p->vxlan.udp.sp);
773                         d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
774                         d->udp.dgram_len = 0; /* not pre-computed */
775                         d->udp.dgram_cksum = 0;
776
777                         /* VXLAN */
778                         d->vxlan.vx_flags = rte_htonl(0x08000000);
779                         d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
780
781                         return 0;
782                 } else {
783                         struct encap_vxlan_ipv4_data *d = data;
784
785                         /* Ethernet */
786                         ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
787                         ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
788                         d->ether.ether_type = rte_htons(ETHER_TYPE_IPv4);
789
790                         /* IPv4*/
791                         d->ipv4.version_ihl = 0x45;
792                         d->ipv4.type_of_service = p->vxlan.ipv4.dscp << 2;
793                         d->ipv4.total_length = 0; /* not pre-computed */
794                         d->ipv4.packet_id = 0;
795                         d->ipv4.fragment_offset = 0;
796                         d->ipv4.time_to_live = p->vxlan.ipv4.ttl;
797                         d->ipv4.next_proto_id = IP_PROTO_UDP;
798                         d->ipv4.hdr_checksum = 0;
799                         d->ipv4.src_addr = rte_htonl(p->vxlan.ipv4.sa);
800                         d->ipv4.dst_addr = rte_htonl(p->vxlan.ipv4.da);
801
802                         d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);
803
804                         /* UDP */
805                         d->udp.src_port = rte_htons(p->vxlan.udp.sp);
806                         d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
807                         d->udp.dgram_len = 0; /* not pre-computed */
808                         d->udp.dgram_cksum = 0;
809
810                         /* VXLAN */
811                         d->vxlan.vx_flags = rte_htonl(0x08000000);
812                         d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
813
814                         return 0;
815                 }
816         else
817                 if (cfg->vxlan.vlan) {
818                         struct encap_vxlan_ipv6_vlan_data *d = data;
819
820                         /* Ethernet */
821                         ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
822                         ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
823                         d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);
824
825                         /* VLAN */
826                         d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
827                                 p->vxlan.vlan.dei,
828                                 p->vxlan.vlan.vid));
829                         d->vlan.eth_proto = rte_htons(ETHER_TYPE_IPv6);
830
831                         /* IPv6*/
832                         d->ipv6.vtc_flow = rte_htonl((6 << 28) |
833                                 (p->vxlan.ipv6.dscp << 22) |
834                                 p->vxlan.ipv6.flow_label);
835                         d->ipv6.payload_len = 0; /* not pre-computed */
836                         d->ipv6.proto = IP_PROTO_UDP;
837                         d->ipv6.hop_limits = p->vxlan.ipv6.hop_limit;
838                         memcpy(d->ipv6.src_addr,
839                                 p->vxlan.ipv6.sa,
840                                 sizeof(p->vxlan.ipv6.sa));
841                         memcpy(d->ipv6.dst_addr,
842                                 p->vxlan.ipv6.da,
843                                 sizeof(p->vxlan.ipv6.da));
844
845                         /* UDP */
846                         d->udp.src_port = rte_htons(p->vxlan.udp.sp);
847                         d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
848                         d->udp.dgram_len = 0; /* not pre-computed */
849                         d->udp.dgram_cksum = 0;
850
851                         /* VXLAN */
852                         d->vxlan.vx_flags = rte_htonl(0x08000000);
853                         d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
854
855                         return 0;
856                 } else {
857                         struct encap_vxlan_ipv6_data *d = data;
858
859                         /* Ethernet */
860                         ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
861                         ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
862                         d->ether.ether_type = rte_htons(ETHER_TYPE_IPv6);
863
864                         /* IPv6*/
865                         d->ipv6.vtc_flow = rte_htonl((6 << 28) |
866                                 (p->vxlan.ipv6.dscp << 22) |
867                                 p->vxlan.ipv6.flow_label);
868                         d->ipv6.payload_len = 0; /* not pre-computed */
869                         d->ipv6.proto = IP_PROTO_UDP;
870                         d->ipv6.hop_limits = p->vxlan.ipv6.hop_limit;
871                         memcpy(d->ipv6.src_addr,
872                                 p->vxlan.ipv6.sa,
873                                 sizeof(p->vxlan.ipv6.sa));
874                         memcpy(d->ipv6.dst_addr,
875                                 p->vxlan.ipv6.da,
876                                 sizeof(p->vxlan.ipv6.da));
877
878                         /* UDP */
879                         d->udp.src_port = rte_htons(p->vxlan.udp.sp);
880                         d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
881                         d->udp.dgram_len = 0; /* not pre-computed */
882                         d->udp.dgram_cksum = 0;
883
884                         /* VXLAN */
885                         d->vxlan.vx_flags = rte_htonl(0x08000000);
886                         d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
887
888                         return 0;
889                 }
890 }
891
892 static int
893 encap_apply(void *data,
894         struct rte_table_action_encap_params *p,
895         struct rte_table_action_encap_config *cfg,
896         struct rte_table_action_common_config *common_cfg)
897 {
898         int status;
899
900         /* Check input arguments */
901         status = encap_apply_check(p, cfg);
902         if (status)
903                 return status;
904
905         switch (p->type) {
906         case RTE_TABLE_ACTION_ENCAP_ETHER:
907                 return encap_ether_apply(data, p, common_cfg);
908
909         case RTE_TABLE_ACTION_ENCAP_VLAN:
910                 return encap_vlan_apply(data, p, common_cfg);
911
912         case RTE_TABLE_ACTION_ENCAP_QINQ:
913                 return encap_qinq_apply(data, p, common_cfg);
914
915         case RTE_TABLE_ACTION_ENCAP_MPLS:
916                 return encap_mpls_apply(data, p);
917
918         case RTE_TABLE_ACTION_ENCAP_PPPOE:
919                 return encap_pppoe_apply(data, p);
920
921         case RTE_TABLE_ACTION_ENCAP_VXLAN:
922                 return encap_vxlan_apply(data, p, cfg);
923
924         default:
925                 return -EINVAL;
926         }
927 }
928
929 static __rte_always_inline uint16_t
930 encap_vxlan_ipv4_checksum_update(uint16_t cksum0,
931         uint16_t total_length)
932 {
933         int32_t cksum1;
934
935         cksum1 = cksum0;
936         cksum1 = ~cksum1 & 0xFFFF;
937
938         /* Add total length (one's complement logic) */
939         cksum1 += total_length;
940         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
941         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
942
943         return (uint16_t)(~cksum1);
944 }
945
946 static __rte_always_inline void *
947 encap(void *dst, const void *src, size_t n)
948 {
949         dst = ((uint8_t *) dst) - n;
950         return rte_memcpy(dst, src, n);
951 }
952
953 static __rte_always_inline void
954 pkt_work_encap_vxlan_ipv4(struct rte_mbuf *mbuf,
955         struct encap_vxlan_ipv4_data *vxlan_tbl,
956         struct rte_table_action_encap_config *cfg)
957 {
958         uint32_t ether_offset = cfg->vxlan.data_offset;
959         void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
960         struct encap_vxlan_ipv4_data *vxlan_pkt;
961         uint16_t ether_length, ipv4_total_length, ipv4_hdr_cksum, udp_length;
962
963         ether_length = (uint16_t)mbuf->pkt_len;
964         ipv4_total_length = ether_length +
965                 (sizeof(struct vxlan_hdr) +
966                 sizeof(struct udp_hdr) +
967                 sizeof(struct ipv4_hdr));
968         ipv4_hdr_cksum = encap_vxlan_ipv4_checksum_update(vxlan_tbl->ipv4.hdr_checksum,
969                 rte_htons(ipv4_total_length));
970         udp_length = ether_length +
971                 (sizeof(struct vxlan_hdr) +
972                 sizeof(struct udp_hdr));
973
974         vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
975         vxlan_pkt->ipv4.total_length = rte_htons(ipv4_total_length);
976         vxlan_pkt->ipv4.hdr_checksum = ipv4_hdr_cksum;
977         vxlan_pkt->udp.dgram_len = rte_htons(udp_length);
978
979         mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
980         mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
981 }
982
983 static __rte_always_inline void
984 pkt_work_encap_vxlan_ipv4_vlan(struct rte_mbuf *mbuf,
985         struct encap_vxlan_ipv4_vlan_data *vxlan_tbl,
986         struct rte_table_action_encap_config *cfg)
987 {
988         uint32_t ether_offset = cfg->vxlan.data_offset;
989         void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
990         struct encap_vxlan_ipv4_vlan_data *vxlan_pkt;
991         uint16_t ether_length, ipv4_total_length, ipv4_hdr_cksum, udp_length;
992
993         ether_length = (uint16_t)mbuf->pkt_len;
994         ipv4_total_length = ether_length +
995                 (sizeof(struct vxlan_hdr) +
996                 sizeof(struct udp_hdr) +
997                 sizeof(struct ipv4_hdr));
998         ipv4_hdr_cksum = encap_vxlan_ipv4_checksum_update(vxlan_tbl->ipv4.hdr_checksum,
999                 rte_htons(ipv4_total_length));
1000         udp_length = ether_length +
1001                 (sizeof(struct vxlan_hdr) +
1002                 sizeof(struct udp_hdr));
1003
1004         vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
1005         vxlan_pkt->ipv4.total_length = rte_htons(ipv4_total_length);
1006         vxlan_pkt->ipv4.hdr_checksum = ipv4_hdr_cksum;
1007         vxlan_pkt->udp.dgram_len = rte_htons(udp_length);
1008
1009         mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
1010         mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
1011 }
1012
1013 static __rte_always_inline void
1014 pkt_work_encap_vxlan_ipv6(struct rte_mbuf *mbuf,
1015         struct encap_vxlan_ipv6_data *vxlan_tbl,
1016         struct rte_table_action_encap_config *cfg)
1017 {
1018         uint32_t ether_offset = cfg->vxlan.data_offset;
1019         void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
1020         struct encap_vxlan_ipv6_data *vxlan_pkt;
1021         uint16_t ether_length, ipv6_payload_length, udp_length;
1022
1023         ether_length = (uint16_t)mbuf->pkt_len;
1024         ipv6_payload_length = ether_length +
1025                 (sizeof(struct vxlan_hdr) +
1026                 sizeof(struct udp_hdr));
1027         udp_length = ether_length +
1028                 (sizeof(struct vxlan_hdr) +
1029                 sizeof(struct udp_hdr));
1030
1031         vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
1032         vxlan_pkt->ipv6.payload_len = rte_htons(ipv6_payload_length);
1033         vxlan_pkt->udp.dgram_len = rte_htons(udp_length);
1034
1035         mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
1036         mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
1037 }
1038
1039 static __rte_always_inline void
1040 pkt_work_encap_vxlan_ipv6_vlan(struct rte_mbuf *mbuf,
1041         struct encap_vxlan_ipv6_vlan_data *vxlan_tbl,
1042         struct rte_table_action_encap_config *cfg)
1043 {
1044         uint32_t ether_offset = cfg->vxlan.data_offset;
1045         void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
1046         struct encap_vxlan_ipv6_vlan_data *vxlan_pkt;
1047         uint16_t ether_length, ipv6_payload_length, udp_length;
1048
1049         ether_length = (uint16_t)mbuf->pkt_len;
1050         ipv6_payload_length = ether_length +
1051                 (sizeof(struct vxlan_hdr) +
1052                 sizeof(struct udp_hdr));
1053         udp_length = ether_length +
1054                 (sizeof(struct vxlan_hdr) +
1055                 sizeof(struct udp_hdr));
1056
1057         vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
1058         vxlan_pkt->ipv6.payload_len = rte_htons(ipv6_payload_length);
1059         vxlan_pkt->udp.dgram_len = rte_htons(udp_length);
1060
1061         mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
1062         mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
1063 }
1064
1065 static __rte_always_inline void
1066 pkt_work_encap(struct rte_mbuf *mbuf,
1067         void *data,
1068         struct rte_table_action_encap_config *cfg,
1069         void *ip,
1070         uint16_t total_length,
1071         uint32_t ip_offset)
1072 {
1073         switch (cfg->encap_mask) {
1074         case 1LLU << RTE_TABLE_ACTION_ENCAP_ETHER:
1075                 encap(ip, data, sizeof(struct encap_ether_data));
1076                 mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
1077                         sizeof(struct encap_ether_data));
1078                 mbuf->pkt_len = mbuf->data_len = total_length +
1079                         sizeof(struct encap_ether_data);
1080                 break;
1081
1082         case 1LLU << RTE_TABLE_ACTION_ENCAP_VLAN:
1083                 encap(ip, data, sizeof(struct encap_vlan_data));
1084                 mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
1085                         sizeof(struct encap_vlan_data));
1086                 mbuf->pkt_len = mbuf->data_len = total_length +
1087                         sizeof(struct encap_vlan_data);
1088                 break;
1089
1090         case 1LLU << RTE_TABLE_ACTION_ENCAP_QINQ:
1091                 encap(ip, data, sizeof(struct encap_qinq_data));
1092                 mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
1093                         sizeof(struct encap_qinq_data));
1094                 mbuf->pkt_len = mbuf->data_len = total_length +
1095                         sizeof(struct encap_qinq_data);
1096                 break;
1097
1098         case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
1099         {
1100                 struct encap_mpls_data *mpls = data;
1101                 size_t size = sizeof(struct ether_hdr) +
1102                         mpls->mpls_count * 4;
1103
1104                 encap(ip, data, size);
1105                 mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) + size);
1106                 mbuf->pkt_len = mbuf->data_len = total_length + size;
1107                 break;
1108         }
1109
1110         case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
1111         {
1112                 struct encap_pppoe_data *pppoe =
1113                         encap(ip, data, sizeof(struct encap_pppoe_data));
1114                 pppoe->pppoe_ppp.length = rte_htons(total_length + 2);
1115                 mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
1116                         sizeof(struct encap_pppoe_data));
1117                 mbuf->pkt_len = mbuf->data_len = total_length +
1118                         sizeof(struct encap_pppoe_data);
1119                 break;
1120         }
1121
1122         case 1LLU << RTE_TABLE_ACTION_ENCAP_VXLAN:
1123         {
1124                 if (cfg->vxlan.ip_version)
1125                         if (cfg->vxlan.vlan)
1126                                 pkt_work_encap_vxlan_ipv4_vlan(mbuf, data, cfg);
1127                         else
1128                                 pkt_work_encap_vxlan_ipv4(mbuf, data, cfg);
1129                 else
1130                         if (cfg->vxlan.vlan)
1131                                 pkt_work_encap_vxlan_ipv6_vlan(mbuf, data, cfg);
1132                         else
1133                                 pkt_work_encap_vxlan_ipv6(mbuf, data, cfg);
1134         }
1135
1136         default:
1137                 break;
1138         }
1139 }
1140
1141 /**
1142  * RTE_TABLE_ACTION_NAT
1143  */
1144 static int
1145 nat_cfg_check(struct rte_table_action_nat_config *nat)
1146 {
1147         if ((nat->proto != 0x06) &&
1148                 (nat->proto != 0x11))
1149                 return -ENOTSUP;
1150
1151         return 0;
1152 }
1153
1154 struct nat_ipv4_data {
1155         uint32_t addr;
1156         uint16_t port;
1157 } __attribute__((__packed__));
1158
1159 struct nat_ipv6_data {
1160         uint8_t addr[16];
1161         uint16_t port;
1162 } __attribute__((__packed__));
1163
1164 static size_t
1165 nat_data_size(struct rte_table_action_nat_config *nat __rte_unused,
1166         struct rte_table_action_common_config *common)
1167 {
1168         int ip_version = common->ip_version;
1169
1170         return (ip_version) ?
1171                 sizeof(struct nat_ipv4_data) :
1172                 sizeof(struct nat_ipv6_data);
1173 }
1174
1175 static int
1176 nat_apply_check(struct rte_table_action_nat_params *p,
1177         struct rte_table_action_common_config *cfg)
1178 {
1179         if ((p->ip_version && (cfg->ip_version == 0)) ||
1180                 ((p->ip_version == 0) && cfg->ip_version))
1181                 return -EINVAL;
1182
1183         return 0;
1184 }
1185
1186 static int
1187 nat_apply(void *data,
1188         struct rte_table_action_nat_params *p,
1189         struct rte_table_action_common_config *cfg)
1190 {
1191         int status;
1192
1193         /* Check input arguments */
1194         status = nat_apply_check(p, cfg);
1195         if (status)
1196                 return status;
1197
1198         /* Apply */
1199         if (p->ip_version) {
1200                 struct nat_ipv4_data *d = data;
1201
1202                 d->addr = rte_htonl(p->addr.ipv4);
1203                 d->port = rte_htons(p->port);
1204         } else {
1205                 struct nat_ipv6_data *d = data;
1206
1207                 memcpy(d->addr, p->addr.ipv6, sizeof(d->addr));
1208                 d->port = rte_htons(p->port);
1209         }
1210
1211         return 0;
1212 }
1213
1214 static __rte_always_inline uint16_t
1215 nat_ipv4_checksum_update(uint16_t cksum0,
1216         uint32_t ip0,
1217         uint32_t ip1)
1218 {
1219         int32_t cksum1;
1220
1221         cksum1 = cksum0;
1222         cksum1 = ~cksum1 & 0xFFFF;
1223
1224         /* Subtract ip0 (one's complement logic) */
1225         cksum1 -= (ip0 >> 16) + (ip0 & 0xFFFF);
1226         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1227         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1228
1229         /* Add ip1 (one's complement logic) */
1230         cksum1 += (ip1 >> 16) + (ip1 & 0xFFFF);
1231         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1232         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1233
1234         return (uint16_t)(~cksum1);
1235 }
1236
1237 static __rte_always_inline uint16_t
1238 nat_ipv4_tcp_udp_checksum_update(uint16_t cksum0,
1239         uint32_t ip0,
1240         uint32_t ip1,
1241         uint16_t port0,
1242         uint16_t port1)
1243 {
1244         int32_t cksum1;
1245
1246         cksum1 = cksum0;
1247         cksum1 = ~cksum1 & 0xFFFF;
1248
1249         /* Subtract ip0 and port 0 (one's complement logic) */
1250         cksum1 -= (ip0 >> 16) + (ip0 & 0xFFFF) + port0;
1251         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1252         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1253
1254         /* Add ip1 and port1 (one's complement logic) */
1255         cksum1 += (ip1 >> 16) + (ip1 & 0xFFFF) + port1;
1256         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1257         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1258
1259         return (uint16_t)(~cksum1);
1260 }
1261
1262 static __rte_always_inline uint16_t
1263 nat_ipv6_tcp_udp_checksum_update(uint16_t cksum0,
1264         uint16_t *ip0,
1265         uint16_t *ip1,
1266         uint16_t port0,
1267         uint16_t port1)
1268 {
1269         int32_t cksum1;
1270
1271         cksum1 = cksum0;
1272         cksum1 = ~cksum1 & 0xFFFF;
1273
1274         /* Subtract ip0 and port 0 (one's complement logic) */
1275         cksum1 -= ip0[0] + ip0[1] + ip0[2] + ip0[3] +
1276                 ip0[4] + ip0[5] + ip0[6] + ip0[7] + port0;
1277         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1278         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1279
1280         /* Add ip1 and port1 (one's complement logic) */
1281         cksum1 += ip1[0] + ip1[1] + ip1[2] + ip1[3] +
1282                 ip1[4] + ip1[5] + ip1[6] + ip1[7] + port1;
1283         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1284         cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
1285
1286         return (uint16_t)(~cksum1);
1287 }
1288
1289 static __rte_always_inline void
1290 pkt_ipv4_work_nat(struct ipv4_hdr *ip,
1291         struct nat_ipv4_data *data,
1292         struct rte_table_action_nat_config *cfg)
1293 {
1294         if (cfg->source_nat) {
1295                 if (cfg->proto == 0x6) {
1296                         struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1297                         uint16_t ip_cksum, tcp_cksum;
1298
1299                         ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1300                                 ip->src_addr,
1301                                 data->addr);
1302
1303                         tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
1304                                 ip->src_addr,
1305                                 data->addr,
1306                                 tcp->src_port,
1307                                 data->port);
1308
1309                         ip->src_addr = data->addr;
1310                         ip->hdr_checksum = ip_cksum;
1311                         tcp->src_port = data->port;
1312                         tcp->cksum = tcp_cksum;
1313                 } else {
1314                         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1315                         uint16_t ip_cksum, udp_cksum;
1316
1317                         ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1318                                 ip->src_addr,
1319                                 data->addr);
1320
1321                         udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
1322                                 ip->src_addr,
1323                                 data->addr,
1324                                 udp->src_port,
1325                                 data->port);
1326
1327                         ip->src_addr = data->addr;
1328                         ip->hdr_checksum = ip_cksum;
1329                         udp->src_port = data->port;
1330                         if (udp->dgram_cksum)
1331                                 udp->dgram_cksum = udp_cksum;
1332                 }
1333         } else {
1334                 if (cfg->proto == 0x6) {
1335                         struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1336                         uint16_t ip_cksum, tcp_cksum;
1337
1338                         ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1339                                 ip->dst_addr,
1340                                 data->addr);
1341
1342                         tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
1343                                 ip->dst_addr,
1344                                 data->addr,
1345                                 tcp->dst_port,
1346                                 data->port);
1347
1348                         ip->dst_addr = data->addr;
1349                         ip->hdr_checksum = ip_cksum;
1350                         tcp->dst_port = data->port;
1351                         tcp->cksum = tcp_cksum;
1352                 } else {
1353                         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1354                         uint16_t ip_cksum, udp_cksum;
1355
1356                         ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1357                                 ip->dst_addr,
1358                                 data->addr);
1359
1360                         udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
1361                                 ip->dst_addr,
1362                                 data->addr,
1363                                 udp->dst_port,
1364                                 data->port);
1365
1366                         ip->dst_addr = data->addr;
1367                         ip->hdr_checksum = ip_cksum;
1368                         udp->dst_port = data->port;
1369                         if (udp->dgram_cksum)
1370                                 udp->dgram_cksum = udp_cksum;
1371                 }
1372         }
1373 }
1374
1375 static __rte_always_inline void
1376 pkt_ipv6_work_nat(struct ipv6_hdr *ip,
1377         struct nat_ipv6_data *data,
1378         struct rte_table_action_nat_config *cfg)
1379 {
1380         if (cfg->source_nat) {
1381                 if (cfg->proto == 0x6) {
1382                         struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1383                         uint16_t tcp_cksum;
1384
1385                         tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
1386                                 (uint16_t *)ip->src_addr,
1387                                 (uint16_t *)data->addr,
1388                                 tcp->src_port,
1389                                 data->port);
1390
1391                         rte_memcpy(ip->src_addr, data->addr, 16);
1392                         tcp->src_port = data->port;
1393                         tcp->cksum = tcp_cksum;
1394                 } else {
1395                         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1396                         uint16_t udp_cksum;
1397
1398                         udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
1399                                 (uint16_t *)ip->src_addr,
1400                                 (uint16_t *)data->addr,
1401                                 udp->src_port,
1402                                 data->port);
1403
1404                         rte_memcpy(ip->src_addr, data->addr, 16);
1405                         udp->src_port = data->port;
1406                         udp->dgram_cksum = udp_cksum;
1407                 }
1408         } else {
1409                 if (cfg->proto == 0x6) {
1410                         struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1411                         uint16_t tcp_cksum;
1412
1413                         tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
1414                                 (uint16_t *)ip->dst_addr,
1415                                 (uint16_t *)data->addr,
1416                                 tcp->dst_port,
1417                                 data->port);
1418
1419                         rte_memcpy(ip->dst_addr, data->addr, 16);
1420                         tcp->dst_port = data->port;
1421                         tcp->cksum = tcp_cksum;
1422                 } else {
1423                         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1424                         uint16_t udp_cksum;
1425
1426                         udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
1427                                 (uint16_t *)ip->dst_addr,
1428                                 (uint16_t *)data->addr,
1429                                 udp->dst_port,
1430                                 data->port);
1431
1432                         rte_memcpy(ip->dst_addr, data->addr, 16);
1433                         udp->dst_port = data->port;
1434                         udp->dgram_cksum = udp_cksum;
1435                 }
1436         }
1437 }
1438
1439 /**
1440  * RTE_TABLE_ACTION_TTL
1441  */
1442 static int
1443 ttl_cfg_check(struct rte_table_action_ttl_config *ttl)
1444 {
1445         if (ttl->drop == 0)
1446                 return -ENOTSUP;
1447
1448         return 0;
1449 }
1450
1451 struct ttl_data {
1452         uint32_t n_packets;
1453 } __attribute__((__packed__));
1454
1455 #define TTL_INIT(data, decrement)                         \
1456         ((data)->n_packets = (decrement) ? 1 : 0)
1457
1458 #define TTL_DEC_GET(data)                                  \
1459         ((uint8_t)((data)->n_packets & 1))
1460
1461 #define TTL_STATS_RESET(data)                             \
1462         ((data)->n_packets = ((data)->n_packets & 1))
1463
1464 #define TTL_STATS_READ(data)                               \
1465         ((data)->n_packets >> 1)
1466
1467 #define TTL_STATS_ADD(data, value)                        \
1468         ((data)->n_packets =                                  \
1469                 (((((data)->n_packets >> 1) + (value)) << 1) |    \
1470                 ((data)->n_packets & 1)))
1471
1472 static int
1473 ttl_apply(void *data,
1474         struct rte_table_action_ttl_params *p)
1475 {
1476         struct ttl_data *d = data;
1477
1478         TTL_INIT(d, p->decrement);
1479
1480         return 0;
1481 }
1482
1483 static __rte_always_inline uint64_t
1484 pkt_ipv4_work_ttl(struct ipv4_hdr *ip,
1485         struct ttl_data *data)
1486 {
1487         uint32_t drop;
1488         uint16_t cksum = ip->hdr_checksum;
1489         uint8_t ttl = ip->time_to_live;
1490         uint8_t ttl_diff = TTL_DEC_GET(data);
1491
1492         cksum += ttl_diff;
1493         ttl -= ttl_diff;
1494
1495         ip->hdr_checksum = cksum;
1496         ip->time_to_live = ttl;
1497
1498         drop = (ttl == 0) ? 1 : 0;
1499         TTL_STATS_ADD(data, drop);
1500
1501         return drop;
1502 }
1503
1504 static __rte_always_inline uint64_t
1505 pkt_ipv6_work_ttl(struct ipv6_hdr *ip,
1506         struct ttl_data *data)
1507 {
1508         uint32_t drop;
1509         uint8_t ttl = ip->hop_limits;
1510         uint8_t ttl_diff = TTL_DEC_GET(data);
1511
1512         ttl -= ttl_diff;
1513
1514         ip->hop_limits = ttl;
1515
1516         drop = (ttl == 0) ? 1 : 0;
1517         TTL_STATS_ADD(data, drop);
1518
1519         return drop;
1520 }
1521
1522 /**
1523  * RTE_TABLE_ACTION_STATS
1524  */
1525 static int
1526 stats_cfg_check(struct rte_table_action_stats_config *stats)
1527 {
1528         if ((stats->n_packets_enabled == 0) && (stats->n_bytes_enabled == 0))
1529                 return -EINVAL;
1530
1531         return 0;
1532 }
1533
1534 struct stats_data {
1535         uint64_t n_packets;
1536         uint64_t n_bytes;
1537 } __attribute__((__packed__));
1538
1539 static int
1540 stats_apply(struct stats_data *data,
1541         struct rte_table_action_stats_params *p)
1542 {
1543         data->n_packets = p->n_packets;
1544         data->n_bytes = p->n_bytes;
1545
1546         return 0;
1547 }
1548
1549 static __rte_always_inline void
1550 pkt_work_stats(struct stats_data *data,
1551         uint16_t total_length)
1552 {
1553         data->n_packets++;
1554         data->n_bytes += total_length;
1555 }
1556
1557 /**
1558  * RTE_TABLE_ACTION_TIME
1559  */
1560 struct time_data {
1561         uint64_t time;
1562 } __attribute__((__packed__));
1563
1564 static int
1565 time_apply(struct time_data *data,
1566         struct rte_table_action_time_params *p)
1567 {
1568         data->time = p->time;
1569         return 0;
1570 }
1571
1572 static __rte_always_inline void
1573 pkt_work_time(struct time_data *data,
1574         uint64_t time)
1575 {
1576         data->time = time;
1577 }
1578
1579
1580 /**
1581  * RTE_TABLE_ACTION_CRYPTO
1582  */
1583
1584 #define CRYPTO_OP_MASK_CIPHER   0x1
1585 #define CRYPTO_OP_MASK_AUTH     0x2
1586 #define CRYPTO_OP_MASK_AEAD     0x4
1587
1588 struct crypto_op_sym_iv_aad {
1589         struct rte_crypto_op op;
1590         struct rte_crypto_sym_op sym_op;
1591         union {
1592                 struct {
1593                         uint8_t cipher_iv[
1594                                 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1595                         uint8_t auth_iv[
1596                                 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1597                 } cipher_auth;
1598
1599                 struct {
1600                         uint8_t iv[RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1601                         uint8_t aad[RTE_TABLE_ACTION_SYM_CRYPTO_AAD_SIZE_MAX];
1602                 } aead_iv_aad;
1603
1604         } iv_aad;
1605 };
1606
1607 struct sym_crypto_data {
1608
1609         union {
1610                 struct {
1611
1612                         /** Length of cipher iv. */
1613                         uint16_t cipher_iv_len;
1614
1615                         /** Offset from start of IP header to the cipher iv. */
1616                         uint16_t cipher_iv_data_offset;
1617
1618                         /** Length of cipher iv to be updated in the mbuf. */
1619                         uint16_t cipher_iv_update_len;
1620
1621                         /** Offset from start of IP header to the auth iv. */
1622                         uint16_t auth_iv_data_offset;
1623
1624                         /** Length of auth iv in the mbuf. */
1625                         uint16_t auth_iv_len;
1626
1627                         /** Length of auth iv to be updated in the mbuf. */
1628                         uint16_t auth_iv_update_len;
1629
1630                 } cipher_auth;
1631                 struct {
1632
1633                         /** Length of iv. */
1634                         uint16_t iv_len;
1635
1636                         /** Offset from start of IP header to the aead iv. */
1637                         uint16_t iv_data_offset;
1638
1639                         /** Length of iv to be updated in the mbuf. */
1640                         uint16_t iv_update_len;
1641
1642                         /** Length of aad */
1643                         uint16_t aad_len;
1644
1645                         /** Offset from start of IP header to the aad. */
1646                         uint16_t aad_data_offset;
1647
1648                         /** Length of aad to updated in the mbuf. */
1649                         uint16_t aad_update_len;
1650
1651                 } aead;
1652         };
1653
1654         /** Offset from start of IP header to the data. */
1655         uint16_t data_offset;
1656
1657         /** Digest length. */
1658         uint16_t digest_len;
1659
1660         /** block size */
1661         uint16_t block_size;
1662
1663         /** Mask of crypto operation */
1664         uint16_t op_mask;
1665
1666         /** Session pointer. */
1667         struct rte_cryptodev_sym_session *session;
1668
1669         /** Direction of crypto, encrypt or decrypt */
1670         uint16_t direction;
1671
1672         /** Private data size to store cipher iv / aad. */
1673         uint8_t iv_aad_data[32];
1674
1675 } __attribute__((__packed__));
1676
1677 static int
1678 sym_crypto_cfg_check(struct rte_table_action_sym_crypto_config *cfg)
1679 {
1680         if (!rte_cryptodev_pmd_is_valid_dev(cfg->cryptodev_id))
1681                 return -EINVAL;
1682         if (cfg->mp_create == NULL || cfg->mp_init == NULL)
1683                 return -EINVAL;
1684
1685         return 0;
1686 }
1687
1688 static int
1689 get_block_size(const struct rte_crypto_sym_xform *xform, uint8_t cdev_id)
1690 {
1691         struct rte_cryptodev_info dev_info;
1692         const struct rte_cryptodev_capabilities *cap;
1693         uint32_t i;
1694
1695         rte_cryptodev_info_get(cdev_id, &dev_info);
1696
1697         for (i = 0;; i++) {
1698                 cap = &dev_info.capabilities[i];
1699                 if (!cap)
1700                         break;
1701
1702                 if (cap->sym.xform_type != xform->type)
1703                         continue;
1704
1705                 if ((xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
1706                                 (cap->sym.cipher.algo == xform->cipher.algo))
1707                         return cap->sym.cipher.block_size;
1708
1709                 if ((xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) &&
1710                                 (cap->sym.aead.algo == xform->aead.algo))
1711                         return cap->sym.aead.block_size;
1712
1713                 if (xform->type == RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED)
1714                         break;
1715         }
1716
1717         return -1;
1718 }
1719
1720 static int
1721 sym_crypto_apply(struct sym_crypto_data *data,
1722         struct rte_table_action_sym_crypto_config *cfg,
1723         struct rte_table_action_sym_crypto_params *p)
1724 {
1725         const struct rte_crypto_cipher_xform *cipher_xform = NULL;
1726         const struct rte_crypto_auth_xform *auth_xform = NULL;
1727         const struct rte_crypto_aead_xform *aead_xform = NULL;
1728         struct rte_crypto_sym_xform *xform = p->xform;
1729         struct rte_cryptodev_sym_session *session;
1730         int ret;
1731
1732         memset(data, 0, sizeof(*data));
1733
1734         while (xform) {
1735                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1736                         cipher_xform = &xform->cipher;
1737
1738                         if (cipher_xform->iv.length >
1739                                 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX)
1740                                 return -ENOMEM;
1741                         if (cipher_xform->iv.offset !=
1742                                         RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
1743                                 return -EINVAL;
1744
1745                         ret = get_block_size(xform, cfg->cryptodev_id);
1746                         if (ret < 0)
1747                                 return -1;
1748                         data->block_size = (uint16_t)ret;
1749                         data->op_mask |= CRYPTO_OP_MASK_CIPHER;
1750
1751                         data->cipher_auth.cipher_iv_len =
1752                                         cipher_xform->iv.length;
1753                         data->cipher_auth.cipher_iv_data_offset = (uint16_t)
1754                                         p->cipher_auth.cipher_iv_update.offset;
1755                         data->cipher_auth.cipher_iv_update_len = (uint16_t)
1756                                         p->cipher_auth.cipher_iv_update.length;
1757
1758                         rte_memcpy(data->iv_aad_data,
1759                                         p->cipher_auth.cipher_iv.val,
1760                                         p->cipher_auth.cipher_iv.length);
1761
1762                         data->direction = cipher_xform->op;
1763
1764                 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1765                         auth_xform = &xform->auth;
1766                         if (auth_xform->iv.length >
1767                                 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX)
1768                                 return -ENOMEM;
1769                         data->op_mask |= CRYPTO_OP_MASK_AUTH;
1770
1771                         data->cipher_auth.auth_iv_len = auth_xform->iv.length;
1772                         data->cipher_auth.auth_iv_data_offset = (uint16_t)
1773                                         p->cipher_auth.auth_iv_update.offset;
1774                         data->cipher_auth.auth_iv_update_len = (uint16_t)
1775                                         p->cipher_auth.auth_iv_update.length;
1776                         data->digest_len = auth_xform->digest_length;
1777
1778                         data->direction = (auth_xform->op ==
1779                                         RTE_CRYPTO_AUTH_OP_GENERATE) ?
1780                                         RTE_CRYPTO_CIPHER_OP_ENCRYPT :
1781                                         RTE_CRYPTO_CIPHER_OP_DECRYPT;
1782
1783                 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1784                         aead_xform = &xform->aead;
1785
1786                         if ((aead_xform->iv.length >
1787                                 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX) || (
1788                                 aead_xform->aad_length >
1789                                 RTE_TABLE_ACTION_SYM_CRYPTO_AAD_SIZE_MAX))
1790                                 return -EINVAL;
1791                         if (aead_xform->iv.offset !=
1792                                         RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
1793                                 return -EINVAL;
1794
1795                         ret = get_block_size(xform, cfg->cryptodev_id);
1796                         if (ret < 0)
1797                                 return -1;
1798                         data->block_size = (uint16_t)ret;
1799                         data->op_mask |= CRYPTO_OP_MASK_AEAD;
1800
1801                         data->digest_len = aead_xform->digest_length;
1802                         data->aead.iv_len = aead_xform->iv.length;
1803                         data->aead.aad_len = aead_xform->aad_length;
1804
1805                         data->aead.iv_data_offset = (uint16_t)
1806                                         p->aead.iv_update.offset;
1807                         data->aead.iv_update_len = (uint16_t)
1808                                         p->aead.iv_update.length;
1809                         data->aead.aad_data_offset = (uint16_t)
1810                                         p->aead.aad_update.offset;
1811                         data->aead.aad_update_len = (uint16_t)
1812                                         p->aead.aad_update.length;
1813
1814                         rte_memcpy(data->iv_aad_data,
1815                                         p->aead.iv.val,
1816                                         p->aead.iv.length);
1817
1818                         rte_memcpy(data->iv_aad_data + p->aead.iv.length,
1819                                         p->aead.aad.val,
1820                                         p->aead.aad.length);
1821
1822                         data->direction = (aead_xform->op ==
1823                                         RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1824                                         RTE_CRYPTO_CIPHER_OP_ENCRYPT :
1825                                         RTE_CRYPTO_CIPHER_OP_DECRYPT;
1826                 } else
1827                         return -EINVAL;
1828
1829                 xform = xform->next;
1830         }
1831
1832         if (auth_xform && auth_xform->iv.length) {
1833                 if (cipher_xform) {
1834                         if (auth_xform->iv.offset !=
1835                                         RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET +
1836                                         cipher_xform->iv.length)
1837                                 return -EINVAL;
1838
1839                         rte_memcpy(data->iv_aad_data + cipher_xform->iv.length,
1840                                         p->cipher_auth.auth_iv.val,
1841                                         p->cipher_auth.auth_iv.length);
1842                 } else {
1843                         rte_memcpy(data->iv_aad_data,
1844                                         p->cipher_auth.auth_iv.val,
1845                                         p->cipher_auth.auth_iv.length);
1846                 }
1847         }
1848
1849         session = rte_cryptodev_sym_session_create(cfg->mp_create);
1850         if (!session)
1851                 return -ENOMEM;
1852
1853         ret = rte_cryptodev_sym_session_init(cfg->cryptodev_id, session,
1854                         p->xform, cfg->mp_init);
1855         if (ret < 0) {
1856                 rte_cryptodev_sym_session_free(session);
1857                 return ret;
1858         }
1859
1860         data->data_offset = (uint16_t)p->data_offset;
1861         data->session = session;
1862
1863         return 0;
1864 }
1865
1866 static __rte_always_inline uint64_t
1867 pkt_work_sym_crypto(struct rte_mbuf *mbuf, struct sym_crypto_data *data,
1868                 struct rte_table_action_sym_crypto_config *cfg,
1869                 uint16_t ip_offset)
1870 {
1871         struct crypto_op_sym_iv_aad *crypto_op = (struct crypto_op_sym_iv_aad *)
1872                         RTE_MBUF_METADATA_UINT8_PTR(mbuf, cfg->op_offset);
1873         struct rte_crypto_op *op = &crypto_op->op;
1874         struct rte_crypto_sym_op *sym = op->sym;
1875         uint32_t pkt_offset = sizeof(*mbuf) + mbuf->data_off;
1876         uint32_t payload_len = pkt_offset + mbuf->data_len - data->data_offset;
1877
1878         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1879         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1880         op->phys_addr = mbuf->buf_iova + cfg->op_offset - sizeof(*mbuf);
1881         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1882         sym->m_src = mbuf;
1883         sym->m_dst = NULL;
1884         sym->session = data->session;
1885
1886         /** pad the packet */
1887         if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1888                 uint32_t append_len = RTE_ALIGN_CEIL(payload_len,
1889                                 data->block_size) - payload_len;
1890
1891                 if (unlikely(rte_pktmbuf_append(mbuf, append_len +
1892                                 data->digest_len) == NULL))
1893                         return 1;
1894
1895                 payload_len += append_len;
1896         } else
1897                 payload_len -= data->digest_len;
1898
1899         if (data->op_mask & CRYPTO_OP_MASK_CIPHER) {
1900                 /** prepare cipher op */
1901                 uint8_t *iv = crypto_op->iv_aad.cipher_auth.cipher_iv;
1902
1903                 sym->cipher.data.length = payload_len;
1904                 sym->cipher.data.offset = data->data_offset - pkt_offset;
1905
1906                 if (data->cipher_auth.cipher_iv_update_len) {
1907                         uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
1908                                 data->cipher_auth.cipher_iv_data_offset
1909                                 + ip_offset);
1910
1911                         /** For encryption, update the pkt iv field, otherwise
1912                          *  update the iv_aad_field
1913                          **/
1914                         if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1915                                 rte_memcpy(pkt_iv, data->iv_aad_data,
1916                                         data->cipher_auth.cipher_iv_update_len);
1917                         else
1918                                 rte_memcpy(data->iv_aad_data, pkt_iv,
1919                                         data->cipher_auth.cipher_iv_update_len);
1920                 }
1921
1922                 /** write iv */
1923                 rte_memcpy(iv, data->iv_aad_data,
1924                                 data->cipher_auth.cipher_iv_len);
1925         }
1926
1927         if (data->op_mask & CRYPTO_OP_MASK_AUTH) {
1928                 /** authentication always start from IP header. */
1929                 sym->auth.data.offset = ip_offset - pkt_offset;
1930                 sym->auth.data.length = mbuf->data_len - sym->auth.data.offset -
1931                                 data->digest_len;
1932                 sym->auth.digest.data = rte_pktmbuf_mtod_offset(mbuf,
1933                                 uint8_t *, rte_pktmbuf_pkt_len(mbuf) -
1934                                 data->digest_len);
1935                 sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf,
1936                                 rte_pktmbuf_pkt_len(mbuf) - data->digest_len);
1937
1938                 if (data->cipher_auth.auth_iv_update_len) {
1939                         uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
1940                                         data->cipher_auth.auth_iv_data_offset
1941                                         + ip_offset);
1942                         uint8_t *data_iv = data->iv_aad_data +
1943                                         data->cipher_auth.cipher_iv_len;
1944
1945                         if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1946                                 rte_memcpy(pkt_iv, data_iv,
1947                                         data->cipher_auth.auth_iv_update_len);
1948                         else
1949                                 rte_memcpy(data_iv, pkt_iv,
1950                                         data->cipher_auth.auth_iv_update_len);
1951                 }
1952
1953                 if (data->cipher_auth.auth_iv_len) {
1954                         /** prepare cipher op */
1955                         uint8_t *iv = crypto_op->iv_aad.cipher_auth.auth_iv;
1956
1957                         rte_memcpy(iv, data->iv_aad_data +
1958                                         data->cipher_auth.cipher_iv_len,
1959                                         data->cipher_auth.auth_iv_len);
1960                 }
1961         }
1962
1963         if (data->op_mask & CRYPTO_OP_MASK_AEAD) {
1964                 uint8_t *iv = crypto_op->iv_aad.aead_iv_aad.iv;
1965                 uint8_t *aad = crypto_op->iv_aad.aead_iv_aad.aad;
1966
1967                 sym->aead.aad.data = aad;
1968                 sym->aead.aad.phys_addr = rte_pktmbuf_iova_offset(mbuf,
1969                                 aad - rte_pktmbuf_mtod(mbuf, uint8_t *));
1970                 sym->aead.digest.data = rte_pktmbuf_mtod_offset(mbuf,
1971                                 uint8_t *, rte_pktmbuf_pkt_len(mbuf) -
1972                                 data->digest_len);
1973                 sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf,
1974                                 rte_pktmbuf_pkt_len(mbuf) - data->digest_len);
1975                 sym->aead.data.offset = data->data_offset - pkt_offset;
1976                 sym->aead.data.length = payload_len;
1977
1978                 if (data->aead.iv_update_len) {
1979                         uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
1980                                         data->aead.iv_data_offset + ip_offset);
1981                         uint8_t *data_iv = data->iv_aad_data;
1982
1983                         if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1984                                 rte_memcpy(pkt_iv, data_iv,
1985                                                 data->aead.iv_update_len);
1986                         else
1987                                 rte_memcpy(data_iv, pkt_iv,
1988                                         data->aead.iv_update_len);
1989                 }
1990
1991                 rte_memcpy(iv, data->iv_aad_data, data->aead.iv_len);
1992
1993                 if (data->aead.aad_update_len) {
1994                         uint8_t *pkt_aad = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
1995                                         data->aead.aad_data_offset + ip_offset);
1996                         uint8_t *data_aad = data->iv_aad_data +
1997                                         data->aead.iv_len;
1998
1999                         if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
2000                                 rte_memcpy(pkt_aad, data_aad,
2001                                                 data->aead.iv_update_len);
2002                         else
2003                                 rte_memcpy(data_aad, pkt_aad,
2004                                         data->aead.iv_update_len);
2005                 }
2006
2007                 rte_memcpy(aad, data->iv_aad_data + data->aead.iv_len,
2008                                         data->aead.aad_len);
2009         }
2010
2011         return 0;
2012 }
2013
2014 /**
2015  * RTE_TABLE_ACTION_TAG
2016  */
2017 struct tag_data {
2018         uint32_t tag;
2019 } __attribute__((__packed__));
2020
2021 static int
2022 tag_apply(struct tag_data *data,
2023         struct rte_table_action_tag_params *p)
2024 {
2025         data->tag = p->tag;
2026         return 0;
2027 }
2028
2029 static __rte_always_inline void
2030 pkt_work_tag(struct rte_mbuf *mbuf,
2031         struct tag_data *data)
2032 {
2033         mbuf->hash.fdir.hi = data->tag;
2034         mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2035 }
2036
2037 static __rte_always_inline void
2038 pkt4_work_tag(struct rte_mbuf *mbuf0,
2039         struct rte_mbuf *mbuf1,
2040         struct rte_mbuf *mbuf2,
2041         struct rte_mbuf *mbuf3,
2042         struct tag_data *data0,
2043         struct tag_data *data1,
2044         struct tag_data *data2,
2045         struct tag_data *data3)
2046 {
2047         mbuf0->hash.fdir.hi = data0->tag;
2048         mbuf1->hash.fdir.hi = data1->tag;
2049         mbuf2->hash.fdir.hi = data2->tag;
2050         mbuf3->hash.fdir.hi = data3->tag;
2051
2052         mbuf0->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2053         mbuf1->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2054         mbuf2->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2055         mbuf3->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2056 }
2057
2058 /**
2059  * RTE_TABLE_ACTION_DECAP
2060  */
2061 struct decap_data {
2062         uint16_t n;
2063 } __attribute__((__packed__));
2064
2065 static int
2066 decap_apply(struct decap_data *data,
2067         struct rte_table_action_decap_params *p)
2068 {
2069         data->n = p->n;
2070         return 0;
2071 }
2072
2073 static __rte_always_inline void
2074 pkt_work_decap(struct rte_mbuf *mbuf,
2075         struct decap_data *data)
2076 {
2077         uint16_t data_off = mbuf->data_off;
2078         uint16_t data_len = mbuf->data_len;
2079         uint32_t pkt_len = mbuf->pkt_len;
2080         uint16_t n = data->n;
2081
2082         mbuf->data_off = data_off + n;
2083         mbuf->data_len = data_len - n;
2084         mbuf->pkt_len = pkt_len - n;
2085 }
2086
2087 static __rte_always_inline void
2088 pkt4_work_decap(struct rte_mbuf *mbuf0,
2089         struct rte_mbuf *mbuf1,
2090         struct rte_mbuf *mbuf2,
2091         struct rte_mbuf *mbuf3,
2092         struct decap_data *data0,
2093         struct decap_data *data1,
2094         struct decap_data *data2,
2095         struct decap_data *data3)
2096 {
2097         uint16_t data_off0 = mbuf0->data_off;
2098         uint16_t data_len0 = mbuf0->data_len;
2099         uint32_t pkt_len0 = mbuf0->pkt_len;
2100
2101         uint16_t data_off1 = mbuf1->data_off;
2102         uint16_t data_len1 = mbuf1->data_len;
2103         uint32_t pkt_len1 = mbuf1->pkt_len;
2104
2105         uint16_t data_off2 = mbuf2->data_off;
2106         uint16_t data_len2 = mbuf2->data_len;
2107         uint32_t pkt_len2 = mbuf2->pkt_len;
2108
2109         uint16_t data_off3 = mbuf3->data_off;
2110         uint16_t data_len3 = mbuf3->data_len;
2111         uint32_t pkt_len3 = mbuf3->pkt_len;
2112
2113         uint16_t n0 = data0->n;
2114         uint16_t n1 = data1->n;
2115         uint16_t n2 = data2->n;
2116         uint16_t n3 = data3->n;
2117
2118         mbuf0->data_off = data_off0 + n0;
2119         mbuf0->data_len = data_len0 - n0;
2120         mbuf0->pkt_len = pkt_len0 - n0;
2121
2122         mbuf1->data_off = data_off1 + n1;
2123         mbuf1->data_len = data_len1 - n1;
2124         mbuf1->pkt_len = pkt_len1 - n1;
2125
2126         mbuf2->data_off = data_off2 + n2;
2127         mbuf2->data_len = data_len2 - n2;
2128         mbuf2->pkt_len = pkt_len2 - n2;
2129
2130         mbuf3->data_off = data_off3 + n3;
2131         mbuf3->data_len = data_len3 - n3;
2132         mbuf3->pkt_len = pkt_len3 - n3;
2133 }
2134
2135 /**
2136  * Action profile
2137  */
2138 static int
2139 action_valid(enum rte_table_action_type action)
2140 {
2141         switch (action) {
2142         case RTE_TABLE_ACTION_FWD:
2143         case RTE_TABLE_ACTION_LB:
2144         case RTE_TABLE_ACTION_MTR:
2145         case RTE_TABLE_ACTION_TM:
2146         case RTE_TABLE_ACTION_ENCAP:
2147         case RTE_TABLE_ACTION_NAT:
2148         case RTE_TABLE_ACTION_TTL:
2149         case RTE_TABLE_ACTION_STATS:
2150         case RTE_TABLE_ACTION_TIME:
2151         case RTE_TABLE_ACTION_SYM_CRYPTO:
2152         case RTE_TABLE_ACTION_TAG:
2153         case RTE_TABLE_ACTION_DECAP:
2154                 return 1;
2155         default:
2156                 return 0;
2157         }
2158 }
2159
2160
2161 #define RTE_TABLE_ACTION_MAX                      64
2162
2163 struct ap_config {
2164         uint64_t action_mask;
2165         struct rte_table_action_common_config common;
2166         struct rte_table_action_lb_config lb;
2167         struct rte_table_action_mtr_config mtr;
2168         struct rte_table_action_tm_config tm;
2169         struct rte_table_action_encap_config encap;
2170         struct rte_table_action_nat_config nat;
2171         struct rte_table_action_ttl_config ttl;
2172         struct rte_table_action_stats_config stats;
2173         struct rte_table_action_sym_crypto_config sym_crypto;
2174 };
2175
2176 static size_t
2177 action_cfg_size(enum rte_table_action_type action)
2178 {
2179         switch (action) {
2180         case RTE_TABLE_ACTION_LB:
2181                 return sizeof(struct rte_table_action_lb_config);
2182         case RTE_TABLE_ACTION_MTR:
2183                 return sizeof(struct rte_table_action_mtr_config);
2184         case RTE_TABLE_ACTION_TM:
2185                 return sizeof(struct rte_table_action_tm_config);
2186         case RTE_TABLE_ACTION_ENCAP:
2187                 return sizeof(struct rte_table_action_encap_config);
2188         case RTE_TABLE_ACTION_NAT:
2189                 return sizeof(struct rte_table_action_nat_config);
2190         case RTE_TABLE_ACTION_TTL:
2191                 return sizeof(struct rte_table_action_ttl_config);
2192         case RTE_TABLE_ACTION_STATS:
2193                 return sizeof(struct rte_table_action_stats_config);
2194         case RTE_TABLE_ACTION_SYM_CRYPTO:
2195                 return sizeof(struct rte_table_action_sym_crypto_config);
2196         default:
2197                 return 0;
2198         }
2199 }
2200
2201 static void*
2202 action_cfg_get(struct ap_config *ap_config,
2203         enum rte_table_action_type type)
2204 {
2205         switch (type) {
2206         case RTE_TABLE_ACTION_LB:
2207                 return &ap_config->lb;
2208
2209         case RTE_TABLE_ACTION_MTR:
2210                 return &ap_config->mtr;
2211
2212         case RTE_TABLE_ACTION_TM:
2213                 return &ap_config->tm;
2214
2215         case RTE_TABLE_ACTION_ENCAP:
2216                 return &ap_config->encap;
2217
2218         case RTE_TABLE_ACTION_NAT:
2219                 return &ap_config->nat;
2220
2221         case RTE_TABLE_ACTION_TTL:
2222                 return &ap_config->ttl;
2223
2224         case RTE_TABLE_ACTION_STATS:
2225                 return &ap_config->stats;
2226
2227         case RTE_TABLE_ACTION_SYM_CRYPTO:
2228                 return &ap_config->sym_crypto;
2229         default:
2230                 return NULL;
2231         }
2232 }
2233
2234 static void
2235 action_cfg_set(struct ap_config *ap_config,
2236         enum rte_table_action_type type,
2237         void *action_cfg)
2238 {
2239         void *dst = action_cfg_get(ap_config, type);
2240
2241         if (dst)
2242                 memcpy(dst, action_cfg, action_cfg_size(type));
2243
2244         ap_config->action_mask |= 1LLU << type;
2245 }
2246
2247 struct ap_data {
2248         size_t offset[RTE_TABLE_ACTION_MAX];
2249         size_t total_size;
2250 };
2251
2252 static size_t
2253 action_data_size(enum rte_table_action_type action,
2254         struct ap_config *ap_config)
2255 {
2256         switch (action) {
2257         case RTE_TABLE_ACTION_FWD:
2258                 return sizeof(struct fwd_data);
2259
2260         case RTE_TABLE_ACTION_LB:
2261                 return sizeof(struct lb_data);
2262
2263         case RTE_TABLE_ACTION_MTR:
2264                 return mtr_data_size(&ap_config->mtr);
2265
2266         case RTE_TABLE_ACTION_TM:
2267                 return sizeof(struct tm_data);
2268
2269         case RTE_TABLE_ACTION_ENCAP:
2270                 return encap_data_size(&ap_config->encap);
2271
2272         case RTE_TABLE_ACTION_NAT:
2273                 return nat_data_size(&ap_config->nat,
2274                         &ap_config->common);
2275
2276         case RTE_TABLE_ACTION_TTL:
2277                 return sizeof(struct ttl_data);
2278
2279         case RTE_TABLE_ACTION_STATS:
2280                 return sizeof(struct stats_data);
2281
2282         case RTE_TABLE_ACTION_TIME:
2283                 return sizeof(struct time_data);
2284
2285         case RTE_TABLE_ACTION_SYM_CRYPTO:
2286                 return (sizeof(struct sym_crypto_data));
2287
2288         case RTE_TABLE_ACTION_TAG:
2289                 return sizeof(struct tag_data);
2290
2291         case RTE_TABLE_ACTION_DECAP:
2292                 return sizeof(struct decap_data);
2293
2294         default:
2295                 return 0;
2296         }
2297 }
2298
2299
2300 static void
2301 action_data_offset_set(struct ap_data *ap_data,
2302         struct ap_config *ap_config)
2303 {
2304         uint64_t action_mask = ap_config->action_mask;
2305         size_t offset;
2306         uint32_t action;
2307
2308         memset(ap_data->offset, 0, sizeof(ap_data->offset));
2309
2310         offset = 0;
2311         for (action = 0; action < RTE_TABLE_ACTION_MAX; action++)
2312                 if (action_mask & (1LLU << action)) {
2313                         ap_data->offset[action] = offset;
2314                         offset += action_data_size((enum rte_table_action_type)action,
2315                                 ap_config);
2316                 }
2317
2318         ap_data->total_size = offset;
2319 }
2320
2321 struct rte_table_action_profile {
2322         struct ap_config cfg;
2323         struct ap_data data;
2324         int frozen;
2325 };
2326
2327 struct rte_table_action_profile *
2328 rte_table_action_profile_create(struct rte_table_action_common_config *common)
2329 {
2330         struct rte_table_action_profile *ap;
2331
2332         /* Check input arguments */
2333         if (common == NULL)
2334                 return NULL;
2335
2336         /* Memory allocation */
2337         ap = calloc(1, sizeof(struct rte_table_action_profile));
2338         if (ap == NULL)
2339                 return NULL;
2340
2341         /* Initialization */
2342         memcpy(&ap->cfg.common, common, sizeof(*common));
2343
2344         return ap;
2345 }
2346
2347
2348 int
2349 rte_table_action_profile_action_register(struct rte_table_action_profile *profile,
2350         enum rte_table_action_type type,
2351         void *action_config)
2352 {
2353         int status;
2354
2355         /* Check input arguments */
2356         if ((profile == NULL) ||
2357                 profile->frozen ||
2358                 (action_valid(type) == 0) ||
2359                 (profile->cfg.action_mask & (1LLU << type)) ||
2360                 ((action_cfg_size(type) == 0) && action_config) ||
2361                 (action_cfg_size(type) && (action_config == NULL)))
2362                 return -EINVAL;
2363
2364         switch (type) {
2365         case RTE_TABLE_ACTION_LB:
2366                 status = lb_cfg_check(action_config);
2367                 break;
2368
2369         case RTE_TABLE_ACTION_MTR:
2370                 status = mtr_cfg_check(action_config);
2371                 break;
2372
2373         case RTE_TABLE_ACTION_TM:
2374                 status = tm_cfg_check(action_config);
2375                 break;
2376
2377         case RTE_TABLE_ACTION_ENCAP:
2378                 status = encap_cfg_check(action_config);
2379                 break;
2380
2381         case RTE_TABLE_ACTION_NAT:
2382                 status = nat_cfg_check(action_config);
2383                 break;
2384
2385         case RTE_TABLE_ACTION_TTL:
2386                 status = ttl_cfg_check(action_config);
2387                 break;
2388
2389         case RTE_TABLE_ACTION_STATS:
2390                 status = stats_cfg_check(action_config);
2391                 break;
2392
2393         case RTE_TABLE_ACTION_SYM_CRYPTO:
2394                 status = sym_crypto_cfg_check(action_config);
2395                 break;
2396
2397         default:
2398                 status = 0;
2399                 break;
2400         }
2401
2402         if (status)
2403                 return status;
2404
2405         /* Action enable */
2406         action_cfg_set(&profile->cfg, type, action_config);
2407
2408         return 0;
2409 }
2410
2411 int
2412 rte_table_action_profile_freeze(struct rte_table_action_profile *profile)
2413 {
2414         if (profile->frozen)
2415                 return -EBUSY;
2416
2417         profile->cfg.action_mask |= 1LLU << RTE_TABLE_ACTION_FWD;
2418         action_data_offset_set(&profile->data, &profile->cfg);
2419         profile->frozen = 1;
2420
2421         return 0;
2422 }
2423
2424 int
2425 rte_table_action_profile_free(struct rte_table_action_profile *profile)
2426 {
2427         if (profile == NULL)
2428                 return 0;
2429
2430         free(profile);
2431         return 0;
2432 }
2433
2434 /**
2435  * Action
2436  */
2437 #define METER_PROFILES_MAX                                 32
2438
2439 struct rte_table_action {
2440         struct ap_config cfg;
2441         struct ap_data data;
2442         struct dscp_table_data dscp_table;
2443         struct meter_profile_data mp[METER_PROFILES_MAX];
2444 };
2445
2446 struct rte_table_action *
2447 rte_table_action_create(struct rte_table_action_profile *profile,
2448         uint32_t socket_id)
2449 {
2450         struct rte_table_action *action;
2451
2452         /* Check input arguments */
2453         if ((profile == NULL) ||
2454                 (profile->frozen == 0))
2455                 return NULL;
2456
2457         /* Memory allocation */
2458         action = rte_zmalloc_socket(NULL,
2459                 sizeof(struct rte_table_action),
2460                 RTE_CACHE_LINE_SIZE,
2461                 socket_id);
2462         if (action == NULL)
2463                 return NULL;
2464
2465         /* Initialization */
2466         memcpy(&action->cfg, &profile->cfg, sizeof(profile->cfg));
2467         memcpy(&action->data, &profile->data, sizeof(profile->data));
2468
2469         return action;
2470 }
2471
2472 static __rte_always_inline void *
2473 action_data_get(void *data,
2474         struct rte_table_action *action,
2475         enum rte_table_action_type type)
2476 {
2477         size_t offset = action->data.offset[type];
2478         uint8_t *data_bytes = data;
2479
2480         return &data_bytes[offset];
2481 }
2482
2483 int
2484 rte_table_action_apply(struct rte_table_action *action,
2485         void *data,
2486         enum rte_table_action_type type,
2487         void *action_params)
2488 {
2489         void *action_data;
2490
2491         /* Check input arguments */
2492         if ((action == NULL) ||
2493                 (data == NULL) ||
2494                 (action_valid(type) == 0) ||
2495                 ((action->cfg.action_mask & (1LLU << type)) == 0) ||
2496                 (action_params == NULL))
2497                 return -EINVAL;
2498
2499         /* Data update */
2500         action_data = action_data_get(data, action, type);
2501
2502         switch (type) {
2503         case RTE_TABLE_ACTION_FWD:
2504                 return fwd_apply(action_data,
2505                         action_params);
2506
2507         case RTE_TABLE_ACTION_LB:
2508                 return lb_apply(action_data,
2509                         action_params);
2510
2511         case RTE_TABLE_ACTION_MTR:
2512                 return mtr_apply(action_data,
2513                         action_params,
2514                         &action->cfg.mtr,
2515                         action->mp,
2516                         RTE_DIM(action->mp));
2517
2518         case RTE_TABLE_ACTION_TM:
2519                 return tm_apply(action_data,
2520                         action_params,
2521                         &action->cfg.tm);
2522
2523         case RTE_TABLE_ACTION_ENCAP:
2524                 return encap_apply(action_data,
2525                         action_params,
2526                         &action->cfg.encap,
2527                         &action->cfg.common);
2528
2529         case RTE_TABLE_ACTION_NAT:
2530                 return nat_apply(action_data,
2531                         action_params,
2532                         &action->cfg.common);
2533
2534         case RTE_TABLE_ACTION_TTL:
2535                 return ttl_apply(action_data,
2536                         action_params);
2537
2538         case RTE_TABLE_ACTION_STATS:
2539                 return stats_apply(action_data,
2540                         action_params);
2541
2542         case RTE_TABLE_ACTION_TIME:
2543                 return time_apply(action_data,
2544                         action_params);
2545
2546         case RTE_TABLE_ACTION_SYM_CRYPTO:
2547                 return sym_crypto_apply(action_data,
2548                                 &action->cfg.sym_crypto,
2549                                 action_params);
2550
2551         case RTE_TABLE_ACTION_TAG:
2552                 return tag_apply(action_data,
2553                         action_params);
2554
2555         case RTE_TABLE_ACTION_DECAP:
2556                 return decap_apply(action_data,
2557                         action_params);
2558
2559         default:
2560                 return -EINVAL;
2561         }
2562 }
2563
2564 int
2565 rte_table_action_dscp_table_update(struct rte_table_action *action,
2566         uint64_t dscp_mask,
2567         struct rte_table_action_dscp_table *table)
2568 {
2569         uint32_t i;
2570
2571         /* Check input arguments */
2572         if ((action == NULL) ||
2573                 ((action->cfg.action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
2574                 (1LLU << RTE_TABLE_ACTION_TM))) == 0) ||
2575                 (dscp_mask == 0) ||
2576                 (table == NULL))
2577                 return -EINVAL;
2578
2579         for (i = 0; i < RTE_DIM(table->entry); i++) {
2580                 struct dscp_table_entry_data *data =
2581                         &action->dscp_table.entry[i];
2582                 struct rte_table_action_dscp_table_entry *entry =
2583                         &table->entry[i];
2584                 uint16_t queue_tc_color =
2585                         MBUF_SCHED_QUEUE_TC_COLOR(entry->tc_queue_id,
2586                                 entry->tc_id,
2587                                 entry->color);
2588
2589                 if ((dscp_mask & (1LLU << i)) == 0)
2590                         continue;
2591
2592                 data->color = entry->color;
2593                 data->tc = entry->tc_id;
2594                 data->queue_tc_color = queue_tc_color;
2595         }
2596
2597         return 0;
2598 }
2599
2600 int
2601 rte_table_action_meter_profile_add(struct rte_table_action *action,
2602         uint32_t meter_profile_id,
2603         struct rte_table_action_meter_profile *profile)
2604 {
2605         struct meter_profile_data *mp_data;
2606         uint32_t status;
2607
2608         /* Check input arguments */
2609         if ((action == NULL) ||
2610                 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
2611                 (profile == NULL))
2612                 return -EINVAL;
2613
2614         if (profile->alg != RTE_TABLE_ACTION_METER_TRTCM)
2615                 return -ENOTSUP;
2616
2617         mp_data = meter_profile_data_find(action->mp,
2618                 RTE_DIM(action->mp),
2619                 meter_profile_id);
2620         if (mp_data)
2621                 return -EEXIST;
2622
2623         mp_data = meter_profile_data_find_unused(action->mp,
2624                 RTE_DIM(action->mp));
2625         if (!mp_data)
2626                 return -ENOSPC;
2627
2628         /* Install new profile */
2629         status = rte_meter_trtcm_profile_config(&mp_data->profile,
2630                 &profile->trtcm);
2631         if (status)
2632                 return status;
2633
2634         mp_data->profile_id = meter_profile_id;
2635         mp_data->valid = 1;
2636
2637         return 0;
2638 }
2639
2640 int
2641 rte_table_action_meter_profile_delete(struct rte_table_action *action,
2642         uint32_t meter_profile_id)
2643 {
2644         struct meter_profile_data *mp_data;
2645
2646         /* Check input arguments */
2647         if ((action == NULL) ||
2648                 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0))
2649                 return -EINVAL;
2650
2651         mp_data = meter_profile_data_find(action->mp,
2652                 RTE_DIM(action->mp),
2653                 meter_profile_id);
2654         if (!mp_data)
2655                 return 0;
2656
2657         /* Uninstall profile */
2658         mp_data->valid = 0;
2659
2660         return 0;
2661 }
2662
2663 int
2664 rte_table_action_meter_read(struct rte_table_action *action,
2665         void *data,
2666         uint32_t tc_mask,
2667         struct rte_table_action_mtr_counters *stats,
2668         int clear)
2669 {
2670         struct mtr_trtcm_data *mtr_data;
2671         uint32_t i;
2672
2673         /* Check input arguments */
2674         if ((action == NULL) ||
2675                 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
2676                 (data == NULL) ||
2677                 (tc_mask > RTE_LEN2MASK(action->cfg.mtr.n_tc, uint32_t)))
2678                 return -EINVAL;
2679
2680         mtr_data = action_data_get(data, action, RTE_TABLE_ACTION_MTR);
2681
2682         /* Read */
2683         if (stats) {
2684                 for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
2685                         struct rte_table_action_mtr_counters_tc *dst =
2686                                 &stats->stats[i];
2687                         struct mtr_trtcm_data *src = &mtr_data[i];
2688
2689                         if ((tc_mask & (1 << i)) == 0)
2690                                 continue;
2691
2692                         dst->n_packets[e_RTE_METER_GREEN] =
2693                                 mtr_trtcm_data_stats_get(src, e_RTE_METER_GREEN);
2694
2695                         dst->n_packets[e_RTE_METER_YELLOW] =
2696                                 mtr_trtcm_data_stats_get(src, e_RTE_METER_YELLOW);
2697
2698                         dst->n_packets[e_RTE_METER_RED] =
2699                                 mtr_trtcm_data_stats_get(src, e_RTE_METER_RED);
2700
2701                         dst->n_packets_valid = 1;
2702                         dst->n_bytes_valid = 0;
2703                 }
2704
2705                 stats->tc_mask = tc_mask;
2706         }
2707
2708         /* Clear */
2709         if (clear)
2710                 for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
2711                         struct mtr_trtcm_data *src = &mtr_data[i];
2712
2713                         if ((tc_mask & (1 << i)) == 0)
2714                                 continue;
2715
2716                         mtr_trtcm_data_stats_reset(src, e_RTE_METER_GREEN);
2717                         mtr_trtcm_data_stats_reset(src, e_RTE_METER_YELLOW);
2718                         mtr_trtcm_data_stats_reset(src, e_RTE_METER_RED);
2719                 }
2720
2721
2722         return 0;
2723 }
2724
2725 int
2726 rte_table_action_ttl_read(struct rte_table_action *action,
2727         void *data,
2728         struct rte_table_action_ttl_counters *stats,
2729         int clear)
2730 {
2731         struct ttl_data *ttl_data;
2732
2733         /* Check input arguments */
2734         if ((action == NULL) ||
2735                 ((action->cfg.action_mask &
2736                 (1LLU << RTE_TABLE_ACTION_TTL)) == 0) ||
2737                 (data == NULL))
2738                 return -EINVAL;
2739
2740         ttl_data = action_data_get(data, action, RTE_TABLE_ACTION_TTL);
2741
2742         /* Read */
2743         if (stats)
2744                 stats->n_packets = TTL_STATS_READ(ttl_data);
2745
2746         /* Clear */
2747         if (clear)
2748                 TTL_STATS_RESET(ttl_data);
2749
2750         return 0;
2751 }
2752
2753 int
2754 rte_table_action_stats_read(struct rte_table_action *action,
2755         void *data,
2756         struct rte_table_action_stats_counters *stats,
2757         int clear)
2758 {
2759         struct stats_data *stats_data;
2760
2761         /* Check input arguments */
2762         if ((action == NULL) ||
2763                 ((action->cfg.action_mask &
2764                 (1LLU << RTE_TABLE_ACTION_STATS)) == 0) ||
2765                 (data == NULL))
2766                 return -EINVAL;
2767
2768         stats_data = action_data_get(data, action,
2769                 RTE_TABLE_ACTION_STATS);
2770
2771         /* Read */
2772         if (stats) {
2773                 stats->n_packets = stats_data->n_packets;
2774                 stats->n_bytes = stats_data->n_bytes;
2775                 stats->n_packets_valid = 1;
2776                 stats->n_bytes_valid = 1;
2777         }
2778
2779         /* Clear */
2780         if (clear) {
2781                 stats_data->n_packets = 0;
2782                 stats_data->n_bytes = 0;
2783         }
2784
2785         return 0;
2786 }
2787
2788 int
2789 rte_table_action_time_read(struct rte_table_action *action,
2790         void *data,
2791         uint64_t *timestamp)
2792 {
2793         struct time_data *time_data;
2794
2795         /* Check input arguments */
2796         if ((action == NULL) ||
2797                 ((action->cfg.action_mask &
2798                 (1LLU << RTE_TABLE_ACTION_TIME)) == 0) ||
2799                 (data == NULL) ||
2800                 (timestamp == NULL))
2801                 return -EINVAL;
2802
2803         time_data = action_data_get(data, action, RTE_TABLE_ACTION_TIME);
2804
2805         /* Read */
2806         *timestamp = time_data->time;
2807
2808         return 0;
2809 }
2810
2811 struct rte_cryptodev_sym_session *
2812 rte_table_action_crypto_sym_session_get(struct rte_table_action *action,
2813         void *data)
2814 {
2815         struct sym_crypto_data *sym_crypto_data;
2816
2817         /* Check input arguments */
2818         if ((action == NULL) ||
2819                 ((action->cfg.action_mask &
2820                 (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) == 0) ||
2821                 (data == NULL))
2822                 return NULL;
2823
2824         sym_crypto_data = action_data_get(data, action,
2825                         RTE_TABLE_ACTION_SYM_CRYPTO);
2826
2827         return sym_crypto_data->session;
2828 }
2829
2830 static __rte_always_inline uint64_t
2831 pkt_work(struct rte_mbuf *mbuf,
2832         struct rte_pipeline_table_entry *table_entry,
2833         uint64_t time,
2834         struct rte_table_action *action,
2835         struct ap_config *cfg)
2836 {
2837         uint64_t drop_mask = 0;
2838
2839         uint32_t ip_offset = action->cfg.common.ip_offset;
2840         void *ip = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ip_offset);
2841
2842         uint32_t dscp;
2843         uint16_t total_length;
2844
2845         if (cfg->common.ip_version) {
2846                 struct ipv4_hdr *hdr = ip;
2847
2848                 dscp = hdr->type_of_service >> 2;
2849                 total_length = rte_ntohs(hdr->total_length);
2850         } else {
2851                 struct ipv6_hdr *hdr = ip;
2852
2853                 dscp = (rte_ntohl(hdr->vtc_flow) & 0x0F600000) >> 18;
2854                 total_length =
2855                         rte_ntohs(hdr->payload_len) + sizeof(struct ipv6_hdr);
2856         }
2857
2858         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
2859                 void *data =
2860                         action_data_get(table_entry, action, RTE_TABLE_ACTION_LB);
2861
2862                 pkt_work_lb(mbuf,
2863                         data,
2864                         &cfg->lb);
2865         }
2866         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
2867                 void *data =
2868                         action_data_get(table_entry, action, RTE_TABLE_ACTION_MTR);
2869
2870                 drop_mask |= pkt_work_mtr(mbuf,
2871                         data,
2872                         &action->dscp_table,
2873                         action->mp,
2874                         time,
2875                         dscp,
2876                         total_length);
2877         }
2878
2879         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
2880                 void *data =
2881                         action_data_get(table_entry, action, RTE_TABLE_ACTION_TM);
2882
2883                 pkt_work_tm(mbuf,
2884                         data,
2885                         &action->dscp_table,
2886                         dscp);
2887         }
2888
2889         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_DECAP)) {
2890                 void *data = action_data_get(table_entry,
2891                         action,
2892                         RTE_TABLE_ACTION_DECAP);
2893
2894                 pkt_work_decap(mbuf, data);
2895         }
2896
2897         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
2898                 void *data =
2899                         action_data_get(table_entry, action, RTE_TABLE_ACTION_ENCAP);
2900
2901                 pkt_work_encap(mbuf,
2902                         data,
2903                         &cfg->encap,
2904                         ip,
2905                         total_length,
2906                         ip_offset);
2907         }
2908
2909         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
2910                 void *data =
2911                         action_data_get(table_entry, action, RTE_TABLE_ACTION_NAT);
2912
2913                 if (cfg->common.ip_version)
2914                         pkt_ipv4_work_nat(ip, data, &cfg->nat);
2915                 else
2916                         pkt_ipv6_work_nat(ip, data, &cfg->nat);
2917         }
2918
2919         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
2920                 void *data =
2921                         action_data_get(table_entry, action, RTE_TABLE_ACTION_TTL);
2922
2923                 if (cfg->common.ip_version)
2924                         drop_mask |= pkt_ipv4_work_ttl(ip, data);
2925                 else
2926                         drop_mask |= pkt_ipv6_work_ttl(ip, data);
2927         }
2928
2929         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
2930                 void *data =
2931                         action_data_get(table_entry, action, RTE_TABLE_ACTION_STATS);
2932
2933                 pkt_work_stats(data, total_length);
2934         }
2935
2936         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
2937                 void *data =
2938                         action_data_get(table_entry, action, RTE_TABLE_ACTION_TIME);
2939
2940                 pkt_work_time(data, time);
2941         }
2942
2943         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
2944                 void *data = action_data_get(table_entry, action,
2945                                 RTE_TABLE_ACTION_SYM_CRYPTO);
2946
2947                 drop_mask |= pkt_work_sym_crypto(mbuf, data, &cfg->sym_crypto,
2948                                 ip_offset);
2949         }
2950
2951         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TAG)) {
2952                 void *data = action_data_get(table_entry,
2953                         action,
2954                         RTE_TABLE_ACTION_TAG);
2955
2956                 pkt_work_tag(mbuf, data);
2957         }
2958
2959         return drop_mask;
2960 }
2961
2962 static __rte_always_inline uint64_t
2963 pkt4_work(struct rte_mbuf **mbufs,
2964         struct rte_pipeline_table_entry **table_entries,
2965         uint64_t time,
2966         struct rte_table_action *action,
2967         struct ap_config *cfg)
2968 {
2969         uint64_t drop_mask0 = 0;
2970         uint64_t drop_mask1 = 0;
2971         uint64_t drop_mask2 = 0;
2972         uint64_t drop_mask3 = 0;
2973
2974         struct rte_mbuf *mbuf0 = mbufs[0];
2975         struct rte_mbuf *mbuf1 = mbufs[1];
2976         struct rte_mbuf *mbuf2 = mbufs[2];
2977         struct rte_mbuf *mbuf3 = mbufs[3];
2978
2979         struct rte_pipeline_table_entry *table_entry0 = table_entries[0];
2980         struct rte_pipeline_table_entry *table_entry1 = table_entries[1];
2981         struct rte_pipeline_table_entry *table_entry2 = table_entries[2];
2982         struct rte_pipeline_table_entry *table_entry3 = table_entries[3];
2983
2984         uint32_t ip_offset = action->cfg.common.ip_offset;
2985         void *ip0 = RTE_MBUF_METADATA_UINT32_PTR(mbuf0, ip_offset);
2986         void *ip1 = RTE_MBUF_METADATA_UINT32_PTR(mbuf1, ip_offset);
2987         void *ip2 = RTE_MBUF_METADATA_UINT32_PTR(mbuf2, ip_offset);
2988         void *ip3 = RTE_MBUF_METADATA_UINT32_PTR(mbuf3, ip_offset);
2989
2990         uint32_t dscp0, dscp1, dscp2, dscp3;
2991         uint16_t total_length0, total_length1, total_length2, total_length3;
2992
2993         if (cfg->common.ip_version) {
2994                 struct ipv4_hdr *hdr0 = ip0;
2995                 struct ipv4_hdr *hdr1 = ip1;
2996                 struct ipv4_hdr *hdr2 = ip2;
2997                 struct ipv4_hdr *hdr3 = ip3;
2998
2999                 dscp0 = hdr0->type_of_service >> 2;
3000                 dscp1 = hdr1->type_of_service >> 2;
3001                 dscp2 = hdr2->type_of_service >> 2;
3002                 dscp3 = hdr3->type_of_service >> 2;
3003
3004                 total_length0 = rte_ntohs(hdr0->total_length);
3005                 total_length1 = rte_ntohs(hdr1->total_length);
3006                 total_length2 = rte_ntohs(hdr2->total_length);
3007                 total_length3 = rte_ntohs(hdr3->total_length);
3008         } else {
3009                 struct ipv6_hdr *hdr0 = ip0;
3010                 struct ipv6_hdr *hdr1 = ip1;
3011                 struct ipv6_hdr *hdr2 = ip2;
3012                 struct ipv6_hdr *hdr3 = ip3;
3013
3014                 dscp0 = (rte_ntohl(hdr0->vtc_flow) & 0x0F600000) >> 18;
3015                 dscp1 = (rte_ntohl(hdr1->vtc_flow) & 0x0F600000) >> 18;
3016                 dscp2 = (rte_ntohl(hdr2->vtc_flow) & 0x0F600000) >> 18;
3017                 dscp3 = (rte_ntohl(hdr3->vtc_flow) & 0x0F600000) >> 18;
3018
3019                 total_length0 =
3020                         rte_ntohs(hdr0->payload_len) + sizeof(struct ipv6_hdr);
3021                 total_length1 =
3022                         rte_ntohs(hdr1->payload_len) + sizeof(struct ipv6_hdr);
3023                 total_length2 =
3024                         rte_ntohs(hdr2->payload_len) + sizeof(struct ipv6_hdr);
3025                 total_length3 =
3026                         rte_ntohs(hdr3->payload_len) + sizeof(struct ipv6_hdr);
3027         }
3028
3029         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
3030                 void *data0 =
3031                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_LB);
3032                 void *data1 =
3033                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_LB);
3034                 void *data2 =
3035                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_LB);
3036                 void *data3 =
3037                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_LB);
3038
3039                 pkt_work_lb(mbuf0,
3040                         data0,
3041                         &cfg->lb);
3042
3043                 pkt_work_lb(mbuf1,
3044                         data1,
3045                         &cfg->lb);
3046
3047                 pkt_work_lb(mbuf2,
3048                         data2,
3049                         &cfg->lb);
3050
3051                 pkt_work_lb(mbuf3,
3052                         data3,
3053                         &cfg->lb);
3054         }
3055
3056         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
3057                 void *data0 =
3058                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_MTR);
3059                 void *data1 =
3060                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_MTR);
3061                 void *data2 =
3062                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_MTR);
3063                 void *data3 =
3064                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_MTR);
3065
3066                 drop_mask0 |= pkt_work_mtr(mbuf0,
3067                         data0,
3068                         &action->dscp_table,
3069                         action->mp,
3070                         time,
3071                         dscp0,
3072                         total_length0);
3073
3074                 drop_mask1 |= pkt_work_mtr(mbuf1,
3075                         data1,
3076                         &action->dscp_table,
3077                         action->mp,
3078                         time,
3079                         dscp1,
3080                         total_length1);
3081
3082                 drop_mask2 |= pkt_work_mtr(mbuf2,
3083                         data2,
3084                         &action->dscp_table,
3085                         action->mp,
3086                         time,
3087                         dscp2,
3088                         total_length2);
3089
3090                 drop_mask3 |= pkt_work_mtr(mbuf3,
3091                         data3,
3092                         &action->dscp_table,
3093                         action->mp,
3094                         time,
3095                         dscp3,
3096                         total_length3);
3097         }
3098
3099         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
3100                 void *data0 =
3101                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_TM);
3102                 void *data1 =
3103                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_TM);
3104                 void *data2 =
3105                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_TM);
3106                 void *data3 =
3107                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_TM);
3108
3109                 pkt_work_tm(mbuf0,
3110                         data0,
3111                         &action->dscp_table,
3112                         dscp0);
3113
3114                 pkt_work_tm(mbuf1,
3115                         data1,
3116                         &action->dscp_table,
3117                         dscp1);
3118
3119                 pkt_work_tm(mbuf2,
3120                         data2,
3121                         &action->dscp_table,
3122                         dscp2);
3123
3124                 pkt_work_tm(mbuf3,
3125                         data3,
3126                         &action->dscp_table,
3127                         dscp3);
3128         }
3129
3130         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_DECAP)) {
3131                 void *data0 = action_data_get(table_entry0,
3132                         action,
3133                         RTE_TABLE_ACTION_DECAP);
3134                 void *data1 = action_data_get(table_entry1,
3135                         action,
3136                         RTE_TABLE_ACTION_DECAP);
3137                 void *data2 = action_data_get(table_entry2,
3138                         action,
3139                         RTE_TABLE_ACTION_DECAP);
3140                 void *data3 = action_data_get(table_entry3,
3141                         action,
3142                         RTE_TABLE_ACTION_DECAP);
3143
3144                 pkt4_work_decap(mbuf0, mbuf1, mbuf2, mbuf3,
3145                         data0, data1, data2, data3);
3146         }
3147
3148         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
3149                 void *data0 =
3150                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_ENCAP);
3151                 void *data1 =
3152                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_ENCAP);
3153                 void *data2 =
3154                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_ENCAP);
3155                 void *data3 =
3156                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_ENCAP);
3157
3158                 pkt_work_encap(mbuf0,
3159                         data0,
3160                         &cfg->encap,
3161                         ip0,
3162                         total_length0,
3163                         ip_offset);
3164
3165                 pkt_work_encap(mbuf1,
3166                         data1,
3167                         &cfg->encap,
3168                         ip1,
3169                         total_length1,
3170                         ip_offset);
3171
3172                 pkt_work_encap(mbuf2,
3173                         data2,
3174                         &cfg->encap,
3175                         ip2,
3176                         total_length2,
3177                         ip_offset);
3178
3179                 pkt_work_encap(mbuf3,
3180                         data3,
3181                         &cfg->encap,
3182                         ip3,
3183                         total_length3,
3184                         ip_offset);
3185         }
3186
3187         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
3188                 void *data0 =
3189                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_NAT);
3190                 void *data1 =
3191                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_NAT);
3192                 void *data2 =
3193                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_NAT);
3194                 void *data3 =
3195                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_NAT);
3196
3197                 if (cfg->common.ip_version) {
3198                         pkt_ipv4_work_nat(ip0, data0, &cfg->nat);
3199                         pkt_ipv4_work_nat(ip1, data1, &cfg->nat);
3200                         pkt_ipv4_work_nat(ip2, data2, &cfg->nat);
3201                         pkt_ipv4_work_nat(ip3, data3, &cfg->nat);
3202                 } else {
3203                         pkt_ipv6_work_nat(ip0, data0, &cfg->nat);
3204                         pkt_ipv6_work_nat(ip1, data1, &cfg->nat);
3205                         pkt_ipv6_work_nat(ip2, data2, &cfg->nat);
3206                         pkt_ipv6_work_nat(ip3, data3, &cfg->nat);
3207                 }
3208         }
3209
3210         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
3211                 void *data0 =
3212                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_TTL);
3213                 void *data1 =
3214                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_TTL);
3215                 void *data2 =
3216                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_TTL);
3217                 void *data3 =
3218                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_TTL);
3219
3220                 if (cfg->common.ip_version) {
3221                         drop_mask0 |= pkt_ipv4_work_ttl(ip0, data0);
3222                         drop_mask1 |= pkt_ipv4_work_ttl(ip1, data1);
3223                         drop_mask2 |= pkt_ipv4_work_ttl(ip2, data2);
3224                         drop_mask3 |= pkt_ipv4_work_ttl(ip3, data3);
3225                 } else {
3226                         drop_mask0 |= pkt_ipv6_work_ttl(ip0, data0);
3227                         drop_mask1 |= pkt_ipv6_work_ttl(ip1, data1);
3228                         drop_mask2 |= pkt_ipv6_work_ttl(ip2, data2);
3229                         drop_mask3 |= pkt_ipv6_work_ttl(ip3, data3);
3230                 }
3231         }
3232
3233         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
3234                 void *data0 =
3235                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_STATS);
3236                 void *data1 =
3237                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_STATS);
3238                 void *data2 =
3239                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_STATS);
3240                 void *data3 =
3241                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_STATS);
3242
3243                 pkt_work_stats(data0, total_length0);
3244                 pkt_work_stats(data1, total_length1);
3245                 pkt_work_stats(data2, total_length2);
3246                 pkt_work_stats(data3, total_length3);
3247         }
3248
3249         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
3250                 void *data0 =
3251                         action_data_get(table_entry0, action, RTE_TABLE_ACTION_TIME);
3252                 void *data1 =
3253                         action_data_get(table_entry1, action, RTE_TABLE_ACTION_TIME);
3254                 void *data2 =
3255                         action_data_get(table_entry2, action, RTE_TABLE_ACTION_TIME);
3256                 void *data3 =
3257                         action_data_get(table_entry3, action, RTE_TABLE_ACTION_TIME);
3258
3259                 pkt_work_time(data0, time);
3260                 pkt_work_time(data1, time);
3261                 pkt_work_time(data2, time);
3262                 pkt_work_time(data3, time);
3263         }
3264
3265         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
3266                 void *data0 = action_data_get(table_entry0, action,
3267                                 RTE_TABLE_ACTION_SYM_CRYPTO);
3268                 void *data1 = action_data_get(table_entry1, action,
3269                                 RTE_TABLE_ACTION_SYM_CRYPTO);
3270                 void *data2 = action_data_get(table_entry2, action,
3271                                 RTE_TABLE_ACTION_SYM_CRYPTO);
3272                 void *data3 = action_data_get(table_entry3, action,
3273                                 RTE_TABLE_ACTION_SYM_CRYPTO);
3274
3275                 drop_mask0 |= pkt_work_sym_crypto(mbuf0, data0, &cfg->sym_crypto,
3276                                 ip_offset);
3277                 drop_mask1 |= pkt_work_sym_crypto(mbuf1, data1, &cfg->sym_crypto,
3278                                 ip_offset);
3279                 drop_mask2 |= pkt_work_sym_crypto(mbuf2, data2, &cfg->sym_crypto,
3280                                 ip_offset);
3281                 drop_mask3 |= pkt_work_sym_crypto(mbuf3, data3, &cfg->sym_crypto,
3282                                 ip_offset);
3283         }
3284
3285         if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TAG)) {
3286                 void *data0 = action_data_get(table_entry0,
3287                         action,
3288                         RTE_TABLE_ACTION_TAG);
3289                 void *data1 = action_data_get(table_entry1,
3290                         action,
3291                         RTE_TABLE_ACTION_TAG);
3292                 void *data2 = action_data_get(table_entry2,
3293                         action,
3294                         RTE_TABLE_ACTION_TAG);
3295                 void *data3 = action_data_get(table_entry3,
3296                         action,
3297                         RTE_TABLE_ACTION_TAG);
3298
3299                 pkt4_work_tag(mbuf0, mbuf1, mbuf2, mbuf3,
3300                         data0, data1, data2, data3);
3301         }
3302
3303         return drop_mask0 |
3304                 (drop_mask1 << 1) |
3305                 (drop_mask2 << 2) |
3306                 (drop_mask3 << 3);
3307 }
3308
3309 static __rte_always_inline int
3310 ah(struct rte_pipeline *p,
3311         struct rte_mbuf **pkts,
3312         uint64_t pkts_mask,
3313         struct rte_pipeline_table_entry **entries,
3314         struct rte_table_action *action,
3315         struct ap_config *cfg)
3316 {
3317         uint64_t pkts_drop_mask = 0;
3318         uint64_t time = 0;
3319
3320         if (cfg->action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
3321                 (1LLU << RTE_TABLE_ACTION_TIME)))
3322                 time = rte_rdtsc();
3323
3324         if ((pkts_mask & (pkts_mask + 1)) == 0) {
3325                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
3326                 uint32_t i;
3327
3328                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {
3329                         uint64_t drop_mask;
3330
3331                         drop_mask = pkt4_work(&pkts[i],
3332                                 &entries[i],
3333                                 time,
3334                                 action,
3335                                 cfg);
3336
3337                         pkts_drop_mask |= drop_mask << i;
3338                 }
3339
3340                 for ( ; i < n_pkts; i++) {
3341                         uint64_t drop_mask;
3342
3343                         drop_mask = pkt_work(pkts[i],
3344                                 entries[i],
3345                                 time,
3346                                 action,
3347                                 cfg);
3348
3349                         pkts_drop_mask |= drop_mask << i;
3350                 }
3351         } else
3352                 for ( ; pkts_mask; ) {
3353                         uint32_t pos = __builtin_ctzll(pkts_mask);
3354                         uint64_t pkt_mask = 1LLU << pos;
3355                         uint64_t drop_mask;
3356
3357                         drop_mask = pkt_work(pkts[pos],
3358                                 entries[pos],
3359                                 time,
3360                                 action,
3361                                 cfg);
3362
3363                         pkts_mask &= ~pkt_mask;
3364                         pkts_drop_mask |= drop_mask << pos;
3365                 }
3366
3367         rte_pipeline_ah_packet_drop(p, pkts_drop_mask);
3368
3369         return 0;
3370 }
3371
3372 static int
3373 ah_default(struct rte_pipeline *p,
3374         struct rte_mbuf **pkts,
3375         uint64_t pkts_mask,
3376         struct rte_pipeline_table_entry **entries,
3377         void *arg)
3378 {
3379         struct rte_table_action *action = arg;
3380
3381         return ah(p,
3382                 pkts,
3383                 pkts_mask,
3384                 entries,
3385                 action,
3386                 &action->cfg);
3387 }
3388
3389 static rte_pipeline_table_action_handler_hit
3390 ah_selector(struct rte_table_action *action)
3391 {
3392         if (action->cfg.action_mask == (1LLU << RTE_TABLE_ACTION_FWD))
3393                 return NULL;
3394
3395         return ah_default;
3396 }
3397
3398 int
3399 rte_table_action_table_params_get(struct rte_table_action *action,
3400         struct rte_pipeline_table_params *params)
3401 {
3402         rte_pipeline_table_action_handler_hit f_action_hit;
3403         uint32_t total_size;
3404
3405         /* Check input arguments */
3406         if ((action == NULL) ||
3407                 (params == NULL))
3408                 return -EINVAL;
3409
3410         f_action_hit = ah_selector(action);
3411         total_size = rte_align32pow2(action->data.total_size);
3412
3413         /* Fill in params */
3414         params->f_action_hit = f_action_hit;
3415         params->f_action_miss = NULL;
3416         params->arg_ah = (f_action_hit) ? action : NULL;
3417         params->action_data_size = total_size -
3418                 sizeof(struct rte_pipeline_table_entry);
3419
3420         return 0;
3421 }
3422
3423 int
3424 rte_table_action_free(struct rte_table_action *action)
3425 {
3426         if (action == NULL)
3427                 return 0;
3428
3429         rte_free(action);
3430
3431         return 0;
3432 }