1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
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>
17 #include <rte_cryptodev.h>
18 #include <rte_cryptodev_pmd.h>
20 #include "rte_table_action.h"
22 #define rte_htons rte_cpu_to_be_16
23 #define rte_htonl rte_cpu_to_be_32
25 #define rte_ntohs rte_be_to_cpu_16
26 #define rte_ntohl rte_be_to_cpu_32
29 * RTE_TABLE_ACTION_FWD
31 #define fwd_data rte_pipeline_table_entry
34 fwd_apply(struct fwd_data *data,
35 struct rte_table_action_fwd_params *p)
37 data->action = p->action;
39 if (p->action == RTE_PIPELINE_ACTION_PORT)
40 data->port_id = p->id;
42 if (p->action == RTE_PIPELINE_ACTION_TABLE)
43 data->table_id = p->id;
52 lb_cfg_check(struct rte_table_action_lb_config *cfg)
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))
65 uint32_t out[RTE_TABLE_ACTION_LB_TABLE_SIZE];
66 } __attribute__((__packed__));
69 lb_apply(struct lb_data *data,
70 struct rte_table_action_lb_params *p)
72 memcpy(data->out, p->out, sizeof(data->out));
77 static __rte_always_inline void
78 pkt_work_lb(struct rte_mbuf *mbuf,
80 struct rte_table_action_lb_config *cfg)
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);
87 digest = cfg->f_hash(pkt_key,
91 pos = digest & (RTE_TABLE_ACTION_LB_TABLE_SIZE - 1);
92 out_val = data->out[pos];
98 * RTE_TABLE_ACTION_MTR
101 mtr_cfg_check(struct rte_table_action_mtr_config *mtr)
103 if ((mtr->alg == RTE_TABLE_ACTION_METER_SRTCM) ||
104 ((mtr->n_tc != 1) && (mtr->n_tc != 4)) ||
105 (mtr->n_bytes_enabled != 0))
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)))
115 #define MBUF_SCHED_COLOR(sched, color) \
116 (((sched) & (~0x30LLU)) | ((color) << 4))
118 struct mtr_trtcm_data {
119 struct rte_meter_trtcm trtcm;
120 uint64_t stats[e_RTE_METER_COLORS];
121 } __attribute__((__packed__));
123 #define MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data) \
124 (((data)->stats[e_RTE_METER_GREEN] & 0xF8LLU) >> 3)
127 mtr_trtcm_data_meter_profile_id_set(struct mtr_trtcm_data *data,
130 data->stats[e_RTE_METER_GREEN] &= ~0xF8LLU;
131 data->stats[e_RTE_METER_GREEN] |= (profile_id % 32) << 3;
134 #define MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color)\
135 (((data)->stats[(color)] & 4LLU) >> 2)
137 #define MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color)\
138 ((enum rte_meter_color)((data)->stats[(color)] & 3LLU))
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)
145 if (action == RTE_TABLE_ACTION_POLICER_DROP) {
146 data->stats[color] |= 4LLU;
148 data->stats[color] &= ~7LLU;
149 data->stats[color] |= color & 3LLU;
154 mtr_trtcm_data_stats_get(struct mtr_trtcm_data *data,
155 enum rte_meter_color color)
157 return data->stats[color] >> 8;
161 mtr_trtcm_data_stats_reset(struct mtr_trtcm_data *data,
162 enum rte_meter_color color)
164 data->stats[color] &= 0xFFLU;
167 #define MTR_TRTCM_DATA_STATS_INC(data, color) \
168 ((data)->stats[(color)] += (1LLU << 8))
171 mtr_data_size(struct rte_table_action_mtr_config *mtr)
173 return mtr->n_tc * sizeof(struct mtr_trtcm_data);
176 struct dscp_table_entry_data {
177 enum rte_meter_color color;
179 uint16_t queue_tc_color;
182 struct dscp_table_data {
183 struct dscp_table_entry_data entry[64];
186 struct meter_profile_data {
187 struct rte_meter_trtcm_profile profile;
192 static struct meter_profile_data *
193 meter_profile_data_find(struct meter_profile_data *mp,
199 for (i = 0; i < mp_size; i++) {
200 struct meter_profile_data *mp_data = &mp[i];
202 if (mp_data->valid && (mp_data->profile_id == profile_id))
209 static struct meter_profile_data *
210 meter_profile_data_find_unused(struct meter_profile_data *mp,
215 for (i = 0; i < mp_size; i++) {
216 struct meter_profile_data *mp_data = &mp[i];
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,
233 if (p->tc_mask > RTE_LEN2MASK(cfg->n_tc, uint32_t))
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;
240 if ((p->tc_mask & (1LLU << i)) == 0)
243 mp_data = meter_profile_data_find(mp,
245 p_tc->meter_profile_id);
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,
263 /* Check input arguments */
264 status = mtr_apply_check(p, cfg, mp, mp_size);
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;
274 if ((p->tc_mask & (1LLU << i)) == 0)
278 mp_data = meter_profile_data_find(mp,
280 p_tc->meter_profile_id);
284 memset(data_tc, 0, sizeof(*data_tc));
287 status = rte_meter_trtcm_config(&data_tc->trtcm,
293 mtr_trtcm_data_meter_profile_id_set(data_tc,
296 /* Policer actions */
297 mtr_trtcm_data_policer_action_set(data_tc,
299 p_tc->policer[e_RTE_METER_GREEN]);
301 mtr_trtcm_data_policer_action_set(data_tc,
303 p_tc->policer[e_RTE_METER_YELLOW]);
305 mtr_trtcm_data_policer_action_set(data_tc,
307 p_tc->policer[e_RTE_METER_RED]);
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,
320 uint16_t total_length)
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;
329 color_in = dscp_entry->color;
331 mp_id = MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data);
335 color_meter = rte_meter_trtcm_color_aware_check(
343 MTR_TRTCM_DATA_STATS_INC(data, color_meter);
346 drop_mask = MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color_meter);
348 MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color_meter);
349 *sched_ptr = MBUF_SCHED_COLOR(sched, color_policer);
355 * RTE_TABLE_ACTION_TM
358 tm_cfg_check(struct rte_table_action_tm_config *tm)
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))
371 uint16_t queue_tc_color;
374 } __attribute__((__packed__));
377 tm_apply_check(struct rte_table_action_tm_params *p,
378 struct rte_table_action_tm_config *cfg)
380 if ((p->subport_id >= cfg->n_subports_per_port) ||
381 (p->pipe_id >= cfg->n_pipes_per_subport))
388 tm_apply(struct tm_data *data,
389 struct rte_table_action_tm_params *p,
390 struct rte_table_action_tm_config *cfg)
394 /* Check input arguments */
395 status = tm_apply_check(p, cfg);
400 data->queue_tc_color = 0;
401 data->subport = (uint16_t) p->subport_id;
402 data->pipe = p->pipe_id;
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,
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;
418 sched.queue_tc_color = dscp_entry->queue_tc_color;
423 * RTE_TABLE_ACTION_ENCAP
426 encap_valid(enum rte_table_action_encap_type 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:
442 encap_cfg_check(struct rte_table_action_encap_config *encap)
444 if ((encap->encap_mask == 0) ||
445 (__builtin_popcountll(encap->encap_mask) != 1))
451 struct encap_ether_data {
452 struct ether_hdr ether;
453 } __attribute__((__packed__));
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)) \
460 struct encap_vlan_data {
461 struct ether_hdr ether;
462 struct vlan_hdr vlan;
463 } __attribute__((__packed__));
465 struct encap_qinq_data {
466 struct ether_hdr ether;
467 struct vlan_hdr svlan;
468 struct vlan_hdr cvlan;
469 } __attribute__((__packed__));
471 #define ETHER_TYPE_MPLS_UNICAST 0x8847
473 #define ETHER_TYPE_MPLS_MULTICAST 0x8848
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)))
481 struct encap_mpls_data {
482 struct ether_hdr ether;
483 uint32_t mpls[RTE_TABLE_ACTION_MPLS_LABELS_MAX];
485 } __attribute__((__packed__));
487 #define ETHER_TYPE_PPPOE_SESSION 0x8864
489 #define PPP_PROTOCOL_IP 0x0021
491 struct pppoe_ppp_hdr {
492 uint16_t ver_type_code;
496 } __attribute__((__packed__));
498 struct encap_pppoe_data {
499 struct ether_hdr ether;
500 struct pppoe_ppp_hdr pppoe_ppp;
501 } __attribute__((__packed__));
503 #define IP_PROTO_UDP 17
505 struct encap_vxlan_ipv4_data {
506 struct ether_hdr ether;
507 struct ipv4_hdr ipv4;
509 struct vxlan_hdr vxlan;
510 } __attribute__((__packed__));
512 struct encap_vxlan_ipv4_vlan_data {
513 struct ether_hdr ether;
514 struct vlan_hdr vlan;
515 struct ipv4_hdr ipv4;
517 struct vxlan_hdr vxlan;
518 } __attribute__((__packed__));
520 struct encap_vxlan_ipv6_data {
521 struct ether_hdr ether;
522 struct ipv6_hdr ipv6;
524 struct vxlan_hdr vxlan;
525 } __attribute__((__packed__));
527 struct encap_vxlan_ipv6_vlan_data {
528 struct ether_hdr ether;
529 struct vlan_hdr vlan;
530 struct ipv6_hdr ipv6;
532 struct vxlan_hdr vxlan;
533 } __attribute__((__packed__));
536 encap_data_size(struct rte_table_action_encap_config *encap)
538 switch (encap->encap_mask) {
539 case 1LLU << RTE_TABLE_ACTION_ENCAP_ETHER:
540 return sizeof(struct encap_ether_data);
542 case 1LLU << RTE_TABLE_ACTION_ENCAP_VLAN:
543 return sizeof(struct encap_vlan_data);
545 case 1LLU << RTE_TABLE_ACTION_ENCAP_QINQ:
546 return sizeof(struct encap_qinq_data);
548 case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
549 return sizeof(struct encap_mpls_data);
551 case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
552 return sizeof(struct encap_pppoe_data);
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);
559 return sizeof(struct encap_vxlan_ipv4_data);
561 if (encap->vxlan.vlan)
562 return sizeof(struct encap_vxlan_ipv6_vlan_data);
564 return sizeof(struct encap_vxlan_ipv6_data);
572 encap_apply_check(struct rte_table_action_encap_params *p,
573 struct rte_table_action_encap_config *cfg)
575 if ((encap_valid(p->type) == 0) ||
576 ((cfg->encap_mask & (1LLU << p->type)) == 0))
580 case RTE_TABLE_ACTION_ENCAP_ETHER:
583 case RTE_TABLE_ACTION_ENCAP_VLAN:
586 case RTE_TABLE_ACTION_ENCAP_QINQ:
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))
596 case RTE_TABLE_ACTION_ENCAP_PPPOE:
599 case RTE_TABLE_ACTION_ENCAP_VXLAN:
608 encap_ether_apply(void *data,
609 struct rte_table_action_encap_params *p,
610 struct rte_table_action_common_config *common_cfg)
612 struct encap_ether_data *d = data;
613 uint16_t ethertype = (common_cfg->ip_version) ?
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);
626 encap_vlan_apply(void *data,
627 struct rte_table_action_encap_params *p,
628 struct rte_table_action_common_config *common_cfg)
630 struct encap_vlan_data *d = data;
631 uint16_t ethertype = (common_cfg->ip_version) ?
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);
641 d->vlan.vlan_tci = rte_htons(VLAN(p->vlan.vlan.pcp,
644 d->vlan.eth_proto = rte_htons(ethertype);
650 encap_qinq_apply(void *data,
651 struct rte_table_action_encap_params *p,
652 struct rte_table_action_common_config *common_cfg)
654 struct encap_qinq_data *d = data;
655 uint16_t ethertype = (common_cfg->ip_version) ?
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);
665 d->svlan.vlan_tci = rte_htons(VLAN(p->qinq.svlan.pcp,
668 d->svlan.eth_proto = rte_htons(ETHER_TYPE_VLAN);
671 d->cvlan.vlan_tci = rte_htons(VLAN(p->qinq.cvlan.pcp,
674 d->cvlan.eth_proto = rte_htons(ethertype);
680 encap_mpls_apply(void *data,
681 struct rte_table_action_encap_params *p)
683 struct encap_mpls_data *d = data;
684 uint16_t ethertype = (p->mpls.unicast) ?
685 ETHER_TYPE_MPLS_UNICAST :
686 ETHER_TYPE_MPLS_MULTICAST;
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);
695 for (i = 0; i < p->mpls.mpls_count - 1; i++)
696 d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
699 p->mpls.mpls[i].ttl));
701 d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
704 p->mpls.mpls[i].ttl));
706 d->mpls_count = p->mpls.mpls_count;
711 encap_pppoe_apply(void *data,
712 struct rte_table_action_encap_params *p)
714 struct encap_pppoe_data *d = data;
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);
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);
731 encap_vxlan_apply(void *data,
732 struct rte_table_action_encap_params *p,
733 struct rte_table_action_encap_config *cfg)
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)))
742 if (cfg->vxlan.ip_version)
743 if (cfg->vxlan.vlan) {
744 struct encap_vxlan_ipv4_vlan_data *d = data;
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);
752 d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
755 d->vlan.eth_proto = rte_htons(ETHER_TYPE_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);
769 d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);
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;
778 d->vxlan.vx_flags = rte_htonl(0x08000000);
779 d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
783 struct encap_vxlan_ipv4_data *d = data;
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);
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);
802 d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);
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;
811 d->vxlan.vx_flags = rte_htonl(0x08000000);
812 d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
817 if (cfg->vxlan.vlan) {
818 struct encap_vxlan_ipv6_vlan_data *d = data;
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);
826 d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
829 d->vlan.eth_proto = rte_htons(ETHER_TYPE_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,
840 sizeof(p->vxlan.ipv6.sa));
841 memcpy(d->ipv6.dst_addr,
843 sizeof(p->vxlan.ipv6.da));
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;
852 d->vxlan.vx_flags = rte_htonl(0x08000000);
853 d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
857 struct encap_vxlan_ipv6_data *d = data;
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);
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,
873 sizeof(p->vxlan.ipv6.sa));
874 memcpy(d->ipv6.dst_addr,
876 sizeof(p->vxlan.ipv6.da));
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;
885 d->vxlan.vx_flags = rte_htonl(0x08000000);
886 d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);
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)
900 /* Check input arguments */
901 status = encap_apply_check(p, cfg);
906 case RTE_TABLE_ACTION_ENCAP_ETHER:
907 return encap_ether_apply(data, p, common_cfg);
909 case RTE_TABLE_ACTION_ENCAP_VLAN:
910 return encap_vlan_apply(data, p, common_cfg);
912 case RTE_TABLE_ACTION_ENCAP_QINQ:
913 return encap_qinq_apply(data, p, common_cfg);
915 case RTE_TABLE_ACTION_ENCAP_MPLS:
916 return encap_mpls_apply(data, p);
918 case RTE_TABLE_ACTION_ENCAP_PPPOE:
919 return encap_pppoe_apply(data, p);
921 case RTE_TABLE_ACTION_ENCAP_VXLAN:
922 return encap_vxlan_apply(data, p, cfg);
929 static __rte_always_inline uint16_t
930 encap_vxlan_ipv4_checksum_update(uint16_t cksum0,
931 uint16_t total_length)
936 cksum1 = ~cksum1 & 0xFFFF;
938 /* Add total length (one's complement logic) */
939 cksum1 += total_length;
940 cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
941 cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
943 return (uint16_t)(~cksum1);
946 static __rte_always_inline void *
947 encap(void *dst, const void *src, size_t n)
949 dst = ((uint8_t *) dst) - n;
950 return rte_memcpy(dst, src, n);
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)
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;
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));
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);
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);
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)
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;
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));
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);
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);
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)
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;
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));
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);
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);
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)
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;
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));
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);
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);
1065 static __rte_always_inline void
1066 pkt_work_encap(struct rte_mbuf *mbuf,
1068 struct rte_table_action_encap_config *cfg,
1070 uint16_t total_length,
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);
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);
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);
1098 case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
1100 struct encap_mpls_data *mpls = data;
1101 size_t size = sizeof(struct ether_hdr) +
1102 mpls->mpls_count * 4;
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;
1110 case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
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);
1122 case 1LLU << RTE_TABLE_ACTION_ENCAP_VXLAN:
1124 if (cfg->vxlan.ip_version)
1125 if (cfg->vxlan.vlan)
1126 pkt_work_encap_vxlan_ipv4_vlan(mbuf, data, cfg);
1128 pkt_work_encap_vxlan_ipv4(mbuf, data, cfg);
1130 if (cfg->vxlan.vlan)
1131 pkt_work_encap_vxlan_ipv6_vlan(mbuf, data, cfg);
1133 pkt_work_encap_vxlan_ipv6(mbuf, data, cfg);
1142 * RTE_TABLE_ACTION_NAT
1145 nat_cfg_check(struct rte_table_action_nat_config *nat)
1147 if ((nat->proto != 0x06) &&
1148 (nat->proto != 0x11))
1154 struct nat_ipv4_data {
1157 } __attribute__((__packed__));
1159 struct nat_ipv6_data {
1162 } __attribute__((__packed__));
1165 nat_data_size(struct rte_table_action_nat_config *nat __rte_unused,
1166 struct rte_table_action_common_config *common)
1168 int ip_version = common->ip_version;
1170 return (ip_version) ?
1171 sizeof(struct nat_ipv4_data) :
1172 sizeof(struct nat_ipv6_data);
1176 nat_apply_check(struct rte_table_action_nat_params *p,
1177 struct rte_table_action_common_config *cfg)
1179 if ((p->ip_version && (cfg->ip_version == 0)) ||
1180 ((p->ip_version == 0) && cfg->ip_version))
1187 nat_apply(void *data,
1188 struct rte_table_action_nat_params *p,
1189 struct rte_table_action_common_config *cfg)
1193 /* Check input arguments */
1194 status = nat_apply_check(p, cfg);
1199 if (p->ip_version) {
1200 struct nat_ipv4_data *d = data;
1202 d->addr = rte_htonl(p->addr.ipv4);
1203 d->port = rte_htons(p->port);
1205 struct nat_ipv6_data *d = data;
1207 memcpy(d->addr, p->addr.ipv6, sizeof(d->addr));
1208 d->port = rte_htons(p->port);
1214 static __rte_always_inline uint16_t
1215 nat_ipv4_checksum_update(uint16_t cksum0,
1222 cksum1 = ~cksum1 & 0xFFFF;
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);
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);
1234 return (uint16_t)(~cksum1);
1237 static __rte_always_inline uint16_t
1238 nat_ipv4_tcp_udp_checksum_update(uint16_t cksum0,
1247 cksum1 = ~cksum1 & 0xFFFF;
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);
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);
1259 return (uint16_t)(~cksum1);
1262 static __rte_always_inline uint16_t
1263 nat_ipv6_tcp_udp_checksum_update(uint16_t cksum0,
1272 cksum1 = ~cksum1 & 0xFFFF;
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);
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);
1286 return (uint16_t)(~cksum1);
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)
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;
1299 ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1303 tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
1309 ip->src_addr = data->addr;
1310 ip->hdr_checksum = ip_cksum;
1311 tcp->src_port = data->port;
1312 tcp->cksum = tcp_cksum;
1314 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1315 uint16_t ip_cksum, udp_cksum;
1317 ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1321 udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
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;
1334 if (cfg->proto == 0x6) {
1335 struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1336 uint16_t ip_cksum, tcp_cksum;
1338 ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1342 tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
1348 ip->dst_addr = data->addr;
1349 ip->hdr_checksum = ip_cksum;
1350 tcp->dst_port = data->port;
1351 tcp->cksum = tcp_cksum;
1353 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1354 uint16_t ip_cksum, udp_cksum;
1356 ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
1360 udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
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;
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)
1380 if (cfg->source_nat) {
1381 if (cfg->proto == 0x6) {
1382 struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1385 tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
1386 (uint16_t *)ip->src_addr,
1387 (uint16_t *)data->addr,
1391 rte_memcpy(ip->src_addr, data->addr, 16);
1392 tcp->src_port = data->port;
1393 tcp->cksum = tcp_cksum;
1395 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1398 udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
1399 (uint16_t *)ip->src_addr,
1400 (uint16_t *)data->addr,
1404 rte_memcpy(ip->src_addr, data->addr, 16);
1405 udp->src_port = data->port;
1406 udp->dgram_cksum = udp_cksum;
1409 if (cfg->proto == 0x6) {
1410 struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
1413 tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
1414 (uint16_t *)ip->dst_addr,
1415 (uint16_t *)data->addr,
1419 rte_memcpy(ip->dst_addr, data->addr, 16);
1420 tcp->dst_port = data->port;
1421 tcp->cksum = tcp_cksum;
1423 struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
1426 udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
1427 (uint16_t *)ip->dst_addr,
1428 (uint16_t *)data->addr,
1432 rte_memcpy(ip->dst_addr, data->addr, 16);
1433 udp->dst_port = data->port;
1434 udp->dgram_cksum = udp_cksum;
1440 * RTE_TABLE_ACTION_TTL
1443 ttl_cfg_check(struct rte_table_action_ttl_config *ttl)
1453 } __attribute__((__packed__));
1455 #define TTL_INIT(data, decrement) \
1456 ((data)->n_packets = (decrement) ? 1 : 0)
1458 #define TTL_DEC_GET(data) \
1459 ((uint8_t)((data)->n_packets & 1))
1461 #define TTL_STATS_RESET(data) \
1462 ((data)->n_packets = ((data)->n_packets & 1))
1464 #define TTL_STATS_READ(data) \
1465 ((data)->n_packets >> 1)
1467 #define TTL_STATS_ADD(data, value) \
1468 ((data)->n_packets = \
1469 (((((data)->n_packets >> 1) + (value)) << 1) | \
1470 ((data)->n_packets & 1)))
1473 ttl_apply(void *data,
1474 struct rte_table_action_ttl_params *p)
1476 struct ttl_data *d = data;
1478 TTL_INIT(d, p->decrement);
1483 static __rte_always_inline uint64_t
1484 pkt_ipv4_work_ttl(struct ipv4_hdr *ip,
1485 struct ttl_data *data)
1488 uint16_t cksum = ip->hdr_checksum;
1489 uint8_t ttl = ip->time_to_live;
1490 uint8_t ttl_diff = TTL_DEC_GET(data);
1495 ip->hdr_checksum = cksum;
1496 ip->time_to_live = ttl;
1498 drop = (ttl == 0) ? 1 : 0;
1499 TTL_STATS_ADD(data, drop);
1504 static __rte_always_inline uint64_t
1505 pkt_ipv6_work_ttl(struct ipv6_hdr *ip,
1506 struct ttl_data *data)
1509 uint8_t ttl = ip->hop_limits;
1510 uint8_t ttl_diff = TTL_DEC_GET(data);
1514 ip->hop_limits = ttl;
1516 drop = (ttl == 0) ? 1 : 0;
1517 TTL_STATS_ADD(data, drop);
1523 * RTE_TABLE_ACTION_STATS
1526 stats_cfg_check(struct rte_table_action_stats_config *stats)
1528 if ((stats->n_packets_enabled == 0) && (stats->n_bytes_enabled == 0))
1537 } __attribute__((__packed__));
1540 stats_apply(struct stats_data *data,
1541 struct rte_table_action_stats_params *p)
1543 data->n_packets = p->n_packets;
1544 data->n_bytes = p->n_bytes;
1549 static __rte_always_inline void
1550 pkt_work_stats(struct stats_data *data,
1551 uint16_t total_length)
1554 data->n_bytes += total_length;
1558 * RTE_TABLE_ACTION_TIME
1562 } __attribute__((__packed__));
1565 time_apply(struct time_data *data,
1566 struct rte_table_action_time_params *p)
1568 data->time = p->time;
1572 static __rte_always_inline void
1573 pkt_work_time(struct time_data *data,
1581 * RTE_TABLE_ACTION_CRYPTO
1584 #define CRYPTO_OP_MASK_CIPHER 0x1
1585 #define CRYPTO_OP_MASK_AUTH 0x2
1586 #define CRYPTO_OP_MASK_AEAD 0x4
1588 struct crypto_op_sym_iv_aad {
1589 struct rte_crypto_op op;
1590 struct rte_crypto_sym_op sym_op;
1594 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1596 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1600 uint8_t iv[RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
1601 uint8_t aad[RTE_TABLE_ACTION_SYM_CRYPTO_AAD_SIZE_MAX];
1607 struct sym_crypto_data {
1612 /** Length of cipher iv. */
1613 uint16_t cipher_iv_len;
1615 /** Offset from start of IP header to the cipher iv. */
1616 uint16_t cipher_iv_data_offset;
1618 /** Length of cipher iv to be updated in the mbuf. */
1619 uint16_t cipher_iv_update_len;
1621 /** Offset from start of IP header to the auth iv. */
1622 uint16_t auth_iv_data_offset;
1624 /** Length of auth iv in the mbuf. */
1625 uint16_t auth_iv_len;
1627 /** Length of auth iv to be updated in the mbuf. */
1628 uint16_t auth_iv_update_len;
1633 /** Length of iv. */
1636 /** Offset from start of IP header to the aead iv. */
1637 uint16_t iv_data_offset;
1639 /** Length of iv to be updated in the mbuf. */
1640 uint16_t iv_update_len;
1642 /** Length of aad */
1645 /** Offset from start of IP header to the aad. */
1646 uint16_t aad_data_offset;
1648 /** Length of aad to updated in the mbuf. */
1649 uint16_t aad_update_len;
1654 /** Offset from start of IP header to the data. */
1655 uint16_t data_offset;
1657 /** Digest length. */
1658 uint16_t digest_len;
1661 uint16_t block_size;
1663 /** Mask of crypto operation */
1666 /** Session pointer. */
1667 struct rte_cryptodev_sym_session *session;
1669 /** Direction of crypto, encrypt or decrypt */
1672 /** Private data size to store cipher iv / aad. */
1673 uint8_t iv_aad_data[32];
1675 } __attribute__((__packed__));
1678 sym_crypto_cfg_check(struct rte_table_action_sym_crypto_config *cfg)
1680 if (!rte_cryptodev_pmd_is_valid_dev(cfg->cryptodev_id))
1682 if (cfg->mp_create == NULL || cfg->mp_init == NULL)
1689 get_block_size(const struct rte_crypto_sym_xform *xform, uint8_t cdev_id)
1691 struct rte_cryptodev_info dev_info;
1692 const struct rte_cryptodev_capabilities *cap;
1695 rte_cryptodev_info_get(cdev_id, &dev_info);
1698 cap = &dev_info.capabilities[i];
1702 if (cap->sym.xform_type != xform->type)
1705 if ((xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
1706 (cap->sym.cipher.algo == xform->cipher.algo))
1707 return cap->sym.cipher.block_size;
1709 if ((xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) &&
1710 (cap->sym.aead.algo == xform->aead.algo))
1711 return cap->sym.aead.block_size;
1713 if (xform->type == RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED)
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)
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;
1732 memset(data, 0, sizeof(*data));
1735 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1736 cipher_xform = &xform->cipher;
1738 if (cipher_xform->iv.length >
1739 RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX)
1741 if (cipher_xform->iv.offset !=
1742 RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
1745 ret = get_block_size(xform, cfg->cryptodev_id);
1748 data->block_size = (uint16_t)ret;
1749 data->op_mask |= CRYPTO_OP_MASK_CIPHER;
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;
1758 rte_memcpy(data->iv_aad_data,
1759 p->cipher_auth.cipher_iv.val,
1760 p->cipher_auth.cipher_iv.length);
1762 data->direction = cipher_xform->op;
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)
1769 data->op_mask |= CRYPTO_OP_MASK_AUTH;
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;
1778 data->direction = (auth_xform->op ==
1779 RTE_CRYPTO_AUTH_OP_GENERATE) ?
1780 RTE_CRYPTO_CIPHER_OP_ENCRYPT :
1781 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1783 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1784 aead_xform = &xform->aead;
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))
1791 if (aead_xform->iv.offset !=
1792 RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
1795 ret = get_block_size(xform, cfg->cryptodev_id);
1798 data->block_size = (uint16_t)ret;
1799 data->op_mask |= CRYPTO_OP_MASK_AEAD;
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;
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;
1814 rte_memcpy(data->iv_aad_data,
1818 rte_memcpy(data->iv_aad_data + p->aead.iv.length,
1820 p->aead.aad.length);
1822 data->direction = (aead_xform->op ==
1823 RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1824 RTE_CRYPTO_CIPHER_OP_ENCRYPT :
1825 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1829 xform = xform->next;
1832 if (auth_xform && auth_xform->iv.length) {
1834 if (auth_xform->iv.offset !=
1835 RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET +
1836 cipher_xform->iv.length)
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);
1843 rte_memcpy(data->iv_aad_data,
1844 p->cipher_auth.auth_iv.val,
1845 p->cipher_auth.auth_iv.length);
1849 session = rte_cryptodev_sym_session_create(cfg->mp_create);
1853 ret = rte_cryptodev_sym_session_init(cfg->cryptodev_id, session,
1854 p->xform, cfg->mp_init);
1856 rte_cryptodev_sym_session_free(session);
1860 data->data_offset = (uint16_t)p->data_offset;
1861 data->session = session;
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,
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;
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;
1884 sym->session = data->session;
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;
1891 if (unlikely(rte_pktmbuf_append(mbuf, append_len +
1892 data->digest_len) == NULL))
1895 payload_len += append_len;
1897 payload_len -= data->digest_len;
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;
1903 sym->cipher.data.length = payload_len;
1904 sym->cipher.data.offset = data->data_offset - pkt_offset;
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
1911 /** For encryption, update the pkt iv field, otherwise
1912 * update the iv_aad_field
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);
1918 rte_memcpy(data->iv_aad_data, pkt_iv,
1919 data->cipher_auth.cipher_iv_update_len);
1923 rte_memcpy(iv, data->iv_aad_data,
1924 data->cipher_auth.cipher_iv_len);
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 -
1932 sym->auth.digest.data = rte_pktmbuf_mtod_offset(mbuf,
1933 uint8_t *, rte_pktmbuf_pkt_len(mbuf) -
1935 sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf,
1936 rte_pktmbuf_pkt_len(mbuf) - data->digest_len);
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
1942 uint8_t *data_iv = data->iv_aad_data +
1943 data->cipher_auth.cipher_iv_len;
1945 if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1946 rte_memcpy(pkt_iv, data_iv,
1947 data->cipher_auth.auth_iv_update_len);
1949 rte_memcpy(data_iv, pkt_iv,
1950 data->cipher_auth.auth_iv_update_len);
1953 if (data->cipher_auth.auth_iv_len) {
1954 /** prepare cipher op */
1955 uint8_t *iv = crypto_op->iv_aad.cipher_auth.auth_iv;
1957 rte_memcpy(iv, data->iv_aad_data +
1958 data->cipher_auth.cipher_iv_len,
1959 data->cipher_auth.auth_iv_len);
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;
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) -
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;
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;
1983 if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1984 rte_memcpy(pkt_iv, data_iv,
1985 data->aead.iv_update_len);
1987 rte_memcpy(data_iv, pkt_iv,
1988 data->aead.iv_update_len);
1991 rte_memcpy(iv, data->iv_aad_data, data->aead.iv_len);
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 +
1999 if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
2000 rte_memcpy(pkt_aad, data_aad,
2001 data->aead.iv_update_len);
2003 rte_memcpy(data_aad, pkt_aad,
2004 data->aead.iv_update_len);
2007 rte_memcpy(aad, data->iv_aad_data + data->aead.iv_len,
2008 data->aead.aad_len);
2018 action_valid(enum rte_table_action_type action)
2021 case RTE_TABLE_ACTION_FWD:
2022 case RTE_TABLE_ACTION_LB:
2023 case RTE_TABLE_ACTION_MTR:
2024 case RTE_TABLE_ACTION_TM:
2025 case RTE_TABLE_ACTION_ENCAP:
2026 case RTE_TABLE_ACTION_NAT:
2027 case RTE_TABLE_ACTION_TTL:
2028 case RTE_TABLE_ACTION_STATS:
2029 case RTE_TABLE_ACTION_TIME:
2030 case RTE_TABLE_ACTION_SYM_CRYPTO:
2038 #define RTE_TABLE_ACTION_MAX 64
2041 uint64_t action_mask;
2042 struct rte_table_action_common_config common;
2043 struct rte_table_action_lb_config lb;
2044 struct rte_table_action_mtr_config mtr;
2045 struct rte_table_action_tm_config tm;
2046 struct rte_table_action_encap_config encap;
2047 struct rte_table_action_nat_config nat;
2048 struct rte_table_action_ttl_config ttl;
2049 struct rte_table_action_stats_config stats;
2050 struct rte_table_action_sym_crypto_config sym_crypto;
2054 action_cfg_size(enum rte_table_action_type action)
2057 case RTE_TABLE_ACTION_LB:
2058 return sizeof(struct rte_table_action_lb_config);
2059 case RTE_TABLE_ACTION_MTR:
2060 return sizeof(struct rte_table_action_mtr_config);
2061 case RTE_TABLE_ACTION_TM:
2062 return sizeof(struct rte_table_action_tm_config);
2063 case RTE_TABLE_ACTION_ENCAP:
2064 return sizeof(struct rte_table_action_encap_config);
2065 case RTE_TABLE_ACTION_NAT:
2066 return sizeof(struct rte_table_action_nat_config);
2067 case RTE_TABLE_ACTION_TTL:
2068 return sizeof(struct rte_table_action_ttl_config);
2069 case RTE_TABLE_ACTION_STATS:
2070 return sizeof(struct rte_table_action_stats_config);
2071 case RTE_TABLE_ACTION_SYM_CRYPTO:
2072 return sizeof(struct rte_table_action_sym_crypto_config);
2079 action_cfg_get(struct ap_config *ap_config,
2080 enum rte_table_action_type type)
2083 case RTE_TABLE_ACTION_LB:
2084 return &ap_config->lb;
2086 case RTE_TABLE_ACTION_MTR:
2087 return &ap_config->mtr;
2089 case RTE_TABLE_ACTION_TM:
2090 return &ap_config->tm;
2092 case RTE_TABLE_ACTION_ENCAP:
2093 return &ap_config->encap;
2095 case RTE_TABLE_ACTION_NAT:
2096 return &ap_config->nat;
2098 case RTE_TABLE_ACTION_TTL:
2099 return &ap_config->ttl;
2101 case RTE_TABLE_ACTION_STATS:
2102 return &ap_config->stats;
2104 case RTE_TABLE_ACTION_SYM_CRYPTO:
2105 return &ap_config->sym_crypto;
2112 action_cfg_set(struct ap_config *ap_config,
2113 enum rte_table_action_type type,
2116 void *dst = action_cfg_get(ap_config, type);
2119 memcpy(dst, action_cfg, action_cfg_size(type));
2121 ap_config->action_mask |= 1LLU << type;
2125 size_t offset[RTE_TABLE_ACTION_MAX];
2130 action_data_size(enum rte_table_action_type action,
2131 struct ap_config *ap_config)
2134 case RTE_TABLE_ACTION_FWD:
2135 return sizeof(struct fwd_data);
2137 case RTE_TABLE_ACTION_LB:
2138 return sizeof(struct lb_data);
2140 case RTE_TABLE_ACTION_MTR:
2141 return mtr_data_size(&ap_config->mtr);
2143 case RTE_TABLE_ACTION_TM:
2144 return sizeof(struct tm_data);
2146 case RTE_TABLE_ACTION_ENCAP:
2147 return encap_data_size(&ap_config->encap);
2149 case RTE_TABLE_ACTION_NAT:
2150 return nat_data_size(&ap_config->nat,
2151 &ap_config->common);
2153 case RTE_TABLE_ACTION_TTL:
2154 return sizeof(struct ttl_data);
2156 case RTE_TABLE_ACTION_STATS:
2157 return sizeof(struct stats_data);
2159 case RTE_TABLE_ACTION_TIME:
2160 return sizeof(struct time_data);
2162 case RTE_TABLE_ACTION_SYM_CRYPTO:
2163 return (sizeof(struct sym_crypto_data));
2172 action_data_offset_set(struct ap_data *ap_data,
2173 struct ap_config *ap_config)
2175 uint64_t action_mask = ap_config->action_mask;
2179 memset(ap_data->offset, 0, sizeof(ap_data->offset));
2182 for (action = 0; action < RTE_TABLE_ACTION_MAX; action++)
2183 if (action_mask & (1LLU << action)) {
2184 ap_data->offset[action] = offset;
2185 offset += action_data_size((enum rte_table_action_type)action,
2189 ap_data->total_size = offset;
2192 struct rte_table_action_profile {
2193 struct ap_config cfg;
2194 struct ap_data data;
2198 struct rte_table_action_profile *
2199 rte_table_action_profile_create(struct rte_table_action_common_config *common)
2201 struct rte_table_action_profile *ap;
2203 /* Check input arguments */
2207 /* Memory allocation */
2208 ap = calloc(1, sizeof(struct rte_table_action_profile));
2212 /* Initialization */
2213 memcpy(&ap->cfg.common, common, sizeof(*common));
2220 rte_table_action_profile_action_register(struct rte_table_action_profile *profile,
2221 enum rte_table_action_type type,
2222 void *action_config)
2226 /* Check input arguments */
2227 if ((profile == NULL) ||
2229 (action_valid(type) == 0) ||
2230 (profile->cfg.action_mask & (1LLU << type)) ||
2231 ((action_cfg_size(type) == 0) && action_config) ||
2232 (action_cfg_size(type) && (action_config == NULL)))
2236 case RTE_TABLE_ACTION_LB:
2237 status = lb_cfg_check(action_config);
2240 case RTE_TABLE_ACTION_MTR:
2241 status = mtr_cfg_check(action_config);
2244 case RTE_TABLE_ACTION_TM:
2245 status = tm_cfg_check(action_config);
2248 case RTE_TABLE_ACTION_ENCAP:
2249 status = encap_cfg_check(action_config);
2252 case RTE_TABLE_ACTION_NAT:
2253 status = nat_cfg_check(action_config);
2256 case RTE_TABLE_ACTION_TTL:
2257 status = ttl_cfg_check(action_config);
2260 case RTE_TABLE_ACTION_STATS:
2261 status = stats_cfg_check(action_config);
2264 case RTE_TABLE_ACTION_SYM_CRYPTO:
2265 status = sym_crypto_cfg_check(action_config);
2277 action_cfg_set(&profile->cfg, type, action_config);
2283 rte_table_action_profile_freeze(struct rte_table_action_profile *profile)
2285 if (profile->frozen)
2288 profile->cfg.action_mask |= 1LLU << RTE_TABLE_ACTION_FWD;
2289 action_data_offset_set(&profile->data, &profile->cfg);
2290 profile->frozen = 1;
2296 rte_table_action_profile_free(struct rte_table_action_profile *profile)
2298 if (profile == NULL)
2308 #define METER_PROFILES_MAX 32
2310 struct rte_table_action {
2311 struct ap_config cfg;
2312 struct ap_data data;
2313 struct dscp_table_data dscp_table;
2314 struct meter_profile_data mp[METER_PROFILES_MAX];
2317 struct rte_table_action *
2318 rte_table_action_create(struct rte_table_action_profile *profile,
2321 struct rte_table_action *action;
2323 /* Check input arguments */
2324 if ((profile == NULL) ||
2325 (profile->frozen == 0))
2328 /* Memory allocation */
2329 action = rte_zmalloc_socket(NULL,
2330 sizeof(struct rte_table_action),
2331 RTE_CACHE_LINE_SIZE,
2336 /* Initialization */
2337 memcpy(&action->cfg, &profile->cfg, sizeof(profile->cfg));
2338 memcpy(&action->data, &profile->data, sizeof(profile->data));
2343 static __rte_always_inline void *
2344 action_data_get(void *data,
2345 struct rte_table_action *action,
2346 enum rte_table_action_type type)
2348 size_t offset = action->data.offset[type];
2349 uint8_t *data_bytes = data;
2351 return &data_bytes[offset];
2355 rte_table_action_apply(struct rte_table_action *action,
2357 enum rte_table_action_type type,
2358 void *action_params)
2362 /* Check input arguments */
2363 if ((action == NULL) ||
2365 (action_valid(type) == 0) ||
2366 ((action->cfg.action_mask & (1LLU << type)) == 0) ||
2367 (action_params == NULL))
2371 action_data = action_data_get(data, action, type);
2374 case RTE_TABLE_ACTION_FWD:
2375 return fwd_apply(action_data,
2378 case RTE_TABLE_ACTION_LB:
2379 return lb_apply(action_data,
2382 case RTE_TABLE_ACTION_MTR:
2383 return mtr_apply(action_data,
2387 RTE_DIM(action->mp));
2389 case RTE_TABLE_ACTION_TM:
2390 return tm_apply(action_data,
2394 case RTE_TABLE_ACTION_ENCAP:
2395 return encap_apply(action_data,
2398 &action->cfg.common);
2400 case RTE_TABLE_ACTION_NAT:
2401 return nat_apply(action_data,
2403 &action->cfg.common);
2405 case RTE_TABLE_ACTION_TTL:
2406 return ttl_apply(action_data,
2409 case RTE_TABLE_ACTION_STATS:
2410 return stats_apply(action_data,
2413 case RTE_TABLE_ACTION_TIME:
2414 return time_apply(action_data,
2417 case RTE_TABLE_ACTION_SYM_CRYPTO:
2418 return sym_crypto_apply(action_data,
2419 &action->cfg.sym_crypto,
2428 rte_table_action_dscp_table_update(struct rte_table_action *action,
2430 struct rte_table_action_dscp_table *table)
2434 /* Check input arguments */
2435 if ((action == NULL) ||
2436 ((action->cfg.action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
2437 (1LLU << RTE_TABLE_ACTION_TM))) == 0) ||
2442 for (i = 0; i < RTE_DIM(table->entry); i++) {
2443 struct dscp_table_entry_data *data =
2444 &action->dscp_table.entry[i];
2445 struct rte_table_action_dscp_table_entry *entry =
2447 uint16_t queue_tc_color =
2448 MBUF_SCHED_QUEUE_TC_COLOR(entry->tc_queue_id,
2452 if ((dscp_mask & (1LLU << i)) == 0)
2455 data->color = entry->color;
2456 data->tc = entry->tc_id;
2457 data->queue_tc_color = queue_tc_color;
2464 rte_table_action_meter_profile_add(struct rte_table_action *action,
2465 uint32_t meter_profile_id,
2466 struct rte_table_action_meter_profile *profile)
2468 struct meter_profile_data *mp_data;
2471 /* Check input arguments */
2472 if ((action == NULL) ||
2473 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
2477 if (profile->alg != RTE_TABLE_ACTION_METER_TRTCM)
2480 mp_data = meter_profile_data_find(action->mp,
2481 RTE_DIM(action->mp),
2486 mp_data = meter_profile_data_find_unused(action->mp,
2487 RTE_DIM(action->mp));
2491 /* Install new profile */
2492 status = rte_meter_trtcm_profile_config(&mp_data->profile,
2497 mp_data->profile_id = meter_profile_id;
2504 rte_table_action_meter_profile_delete(struct rte_table_action *action,
2505 uint32_t meter_profile_id)
2507 struct meter_profile_data *mp_data;
2509 /* Check input arguments */
2510 if ((action == NULL) ||
2511 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0))
2514 mp_data = meter_profile_data_find(action->mp,
2515 RTE_DIM(action->mp),
2520 /* Uninstall profile */
2527 rte_table_action_meter_read(struct rte_table_action *action,
2530 struct rte_table_action_mtr_counters *stats,
2533 struct mtr_trtcm_data *mtr_data;
2536 /* Check input arguments */
2537 if ((action == NULL) ||
2538 ((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
2540 (tc_mask > RTE_LEN2MASK(action->cfg.mtr.n_tc, uint32_t)))
2543 mtr_data = action_data_get(data, action, RTE_TABLE_ACTION_MTR);
2547 for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
2548 struct rte_table_action_mtr_counters_tc *dst =
2550 struct mtr_trtcm_data *src = &mtr_data[i];
2552 if ((tc_mask & (1 << i)) == 0)
2555 dst->n_packets[e_RTE_METER_GREEN] =
2556 mtr_trtcm_data_stats_get(src, e_RTE_METER_GREEN);
2558 dst->n_packets[e_RTE_METER_YELLOW] =
2559 mtr_trtcm_data_stats_get(src, e_RTE_METER_YELLOW);
2561 dst->n_packets[e_RTE_METER_RED] =
2562 mtr_trtcm_data_stats_get(src, e_RTE_METER_RED);
2564 dst->n_packets_valid = 1;
2565 dst->n_bytes_valid = 0;
2568 stats->tc_mask = tc_mask;
2573 for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
2574 struct mtr_trtcm_data *src = &mtr_data[i];
2576 if ((tc_mask & (1 << i)) == 0)
2579 mtr_trtcm_data_stats_reset(src, e_RTE_METER_GREEN);
2580 mtr_trtcm_data_stats_reset(src, e_RTE_METER_YELLOW);
2581 mtr_trtcm_data_stats_reset(src, e_RTE_METER_RED);
2589 rte_table_action_ttl_read(struct rte_table_action *action,
2591 struct rte_table_action_ttl_counters *stats,
2594 struct ttl_data *ttl_data;
2596 /* Check input arguments */
2597 if ((action == NULL) ||
2598 ((action->cfg.action_mask &
2599 (1LLU << RTE_TABLE_ACTION_TTL)) == 0) ||
2603 ttl_data = action_data_get(data, action, RTE_TABLE_ACTION_TTL);
2607 stats->n_packets = TTL_STATS_READ(ttl_data);
2611 TTL_STATS_RESET(ttl_data);
2617 rte_table_action_stats_read(struct rte_table_action *action,
2619 struct rte_table_action_stats_counters *stats,
2622 struct stats_data *stats_data;
2624 /* Check input arguments */
2625 if ((action == NULL) ||
2626 ((action->cfg.action_mask &
2627 (1LLU << RTE_TABLE_ACTION_STATS)) == 0) ||
2631 stats_data = action_data_get(data, action,
2632 RTE_TABLE_ACTION_STATS);
2636 stats->n_packets = stats_data->n_packets;
2637 stats->n_bytes = stats_data->n_bytes;
2638 stats->n_packets_valid = 1;
2639 stats->n_bytes_valid = 1;
2644 stats_data->n_packets = 0;
2645 stats_data->n_bytes = 0;
2652 rte_table_action_time_read(struct rte_table_action *action,
2654 uint64_t *timestamp)
2656 struct time_data *time_data;
2658 /* Check input arguments */
2659 if ((action == NULL) ||
2660 ((action->cfg.action_mask &
2661 (1LLU << RTE_TABLE_ACTION_TIME)) == 0) ||
2663 (timestamp == NULL))
2666 time_data = action_data_get(data, action, RTE_TABLE_ACTION_TIME);
2669 *timestamp = time_data->time;
2674 struct rte_cryptodev_sym_session *
2675 rte_table_action_crypto_sym_session_get(struct rte_table_action *action,
2678 struct sym_crypto_data *sym_crypto_data;
2680 /* Check input arguments */
2681 if ((action == NULL) ||
2682 ((action->cfg.action_mask &
2683 (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) == 0) ||
2687 sym_crypto_data = action_data_get(data, action,
2688 RTE_TABLE_ACTION_SYM_CRYPTO);
2690 return sym_crypto_data->session;
2693 static __rte_always_inline uint64_t
2694 pkt_work(struct rte_mbuf *mbuf,
2695 struct rte_pipeline_table_entry *table_entry,
2697 struct rte_table_action *action,
2698 struct ap_config *cfg)
2700 uint64_t drop_mask = 0;
2702 uint32_t ip_offset = action->cfg.common.ip_offset;
2703 void *ip = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ip_offset);
2706 uint16_t total_length;
2708 if (cfg->common.ip_version) {
2709 struct ipv4_hdr *hdr = ip;
2711 dscp = hdr->type_of_service >> 2;
2712 total_length = rte_ntohs(hdr->total_length);
2714 struct ipv6_hdr *hdr = ip;
2716 dscp = (rte_ntohl(hdr->vtc_flow) & 0x0F600000) >> 18;
2718 rte_ntohs(hdr->payload_len) + sizeof(struct ipv6_hdr);
2721 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
2723 action_data_get(table_entry, action, RTE_TABLE_ACTION_LB);
2729 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
2731 action_data_get(table_entry, action, RTE_TABLE_ACTION_MTR);
2733 drop_mask |= pkt_work_mtr(mbuf,
2735 &action->dscp_table,
2742 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
2744 action_data_get(table_entry, action, RTE_TABLE_ACTION_TM);
2748 &action->dscp_table,
2752 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
2754 action_data_get(table_entry, action, RTE_TABLE_ACTION_ENCAP);
2756 pkt_work_encap(mbuf,
2764 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
2766 action_data_get(table_entry, action, RTE_TABLE_ACTION_NAT);
2768 if (cfg->common.ip_version)
2769 pkt_ipv4_work_nat(ip, data, &cfg->nat);
2771 pkt_ipv6_work_nat(ip, data, &cfg->nat);
2774 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
2776 action_data_get(table_entry, action, RTE_TABLE_ACTION_TTL);
2778 if (cfg->common.ip_version)
2779 drop_mask |= pkt_ipv4_work_ttl(ip, data);
2781 drop_mask |= pkt_ipv6_work_ttl(ip, data);
2784 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
2786 action_data_get(table_entry, action, RTE_TABLE_ACTION_STATS);
2788 pkt_work_stats(data, total_length);
2791 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
2793 action_data_get(table_entry, action, RTE_TABLE_ACTION_TIME);
2795 pkt_work_time(data, time);
2798 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
2799 void *data = action_data_get(table_entry, action,
2800 RTE_TABLE_ACTION_SYM_CRYPTO);
2802 drop_mask |= pkt_work_sym_crypto(mbuf, data, &cfg->sym_crypto,
2809 static __rte_always_inline uint64_t
2810 pkt4_work(struct rte_mbuf **mbufs,
2811 struct rte_pipeline_table_entry **table_entries,
2813 struct rte_table_action *action,
2814 struct ap_config *cfg)
2816 uint64_t drop_mask0 = 0;
2817 uint64_t drop_mask1 = 0;
2818 uint64_t drop_mask2 = 0;
2819 uint64_t drop_mask3 = 0;
2821 struct rte_mbuf *mbuf0 = mbufs[0];
2822 struct rte_mbuf *mbuf1 = mbufs[1];
2823 struct rte_mbuf *mbuf2 = mbufs[2];
2824 struct rte_mbuf *mbuf3 = mbufs[3];
2826 struct rte_pipeline_table_entry *table_entry0 = table_entries[0];
2827 struct rte_pipeline_table_entry *table_entry1 = table_entries[1];
2828 struct rte_pipeline_table_entry *table_entry2 = table_entries[2];
2829 struct rte_pipeline_table_entry *table_entry3 = table_entries[3];
2831 uint32_t ip_offset = action->cfg.common.ip_offset;
2832 void *ip0 = RTE_MBUF_METADATA_UINT32_PTR(mbuf0, ip_offset);
2833 void *ip1 = RTE_MBUF_METADATA_UINT32_PTR(mbuf1, ip_offset);
2834 void *ip2 = RTE_MBUF_METADATA_UINT32_PTR(mbuf2, ip_offset);
2835 void *ip3 = RTE_MBUF_METADATA_UINT32_PTR(mbuf3, ip_offset);
2837 uint32_t dscp0, dscp1, dscp2, dscp3;
2838 uint16_t total_length0, total_length1, total_length2, total_length3;
2840 if (cfg->common.ip_version) {
2841 struct ipv4_hdr *hdr0 = ip0;
2842 struct ipv4_hdr *hdr1 = ip1;
2843 struct ipv4_hdr *hdr2 = ip2;
2844 struct ipv4_hdr *hdr3 = ip3;
2846 dscp0 = hdr0->type_of_service >> 2;
2847 dscp1 = hdr1->type_of_service >> 2;
2848 dscp2 = hdr2->type_of_service >> 2;
2849 dscp3 = hdr3->type_of_service >> 2;
2851 total_length0 = rte_ntohs(hdr0->total_length);
2852 total_length1 = rte_ntohs(hdr1->total_length);
2853 total_length2 = rte_ntohs(hdr2->total_length);
2854 total_length3 = rte_ntohs(hdr3->total_length);
2856 struct ipv6_hdr *hdr0 = ip0;
2857 struct ipv6_hdr *hdr1 = ip1;
2858 struct ipv6_hdr *hdr2 = ip2;
2859 struct ipv6_hdr *hdr3 = ip3;
2861 dscp0 = (rte_ntohl(hdr0->vtc_flow) & 0x0F600000) >> 18;
2862 dscp1 = (rte_ntohl(hdr1->vtc_flow) & 0x0F600000) >> 18;
2863 dscp2 = (rte_ntohl(hdr2->vtc_flow) & 0x0F600000) >> 18;
2864 dscp3 = (rte_ntohl(hdr3->vtc_flow) & 0x0F600000) >> 18;
2867 rte_ntohs(hdr0->payload_len) + sizeof(struct ipv6_hdr);
2869 rte_ntohs(hdr1->payload_len) + sizeof(struct ipv6_hdr);
2871 rte_ntohs(hdr2->payload_len) + sizeof(struct ipv6_hdr);
2873 rte_ntohs(hdr3->payload_len) + sizeof(struct ipv6_hdr);
2876 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
2878 action_data_get(table_entry0, action, RTE_TABLE_ACTION_LB);
2880 action_data_get(table_entry1, action, RTE_TABLE_ACTION_LB);
2882 action_data_get(table_entry2, action, RTE_TABLE_ACTION_LB);
2884 action_data_get(table_entry3, action, RTE_TABLE_ACTION_LB);
2903 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
2905 action_data_get(table_entry0, action, RTE_TABLE_ACTION_MTR);
2907 action_data_get(table_entry1, action, RTE_TABLE_ACTION_MTR);
2909 action_data_get(table_entry2, action, RTE_TABLE_ACTION_MTR);
2911 action_data_get(table_entry3, action, RTE_TABLE_ACTION_MTR);
2913 drop_mask0 |= pkt_work_mtr(mbuf0,
2915 &action->dscp_table,
2921 drop_mask1 |= pkt_work_mtr(mbuf1,
2923 &action->dscp_table,
2929 drop_mask2 |= pkt_work_mtr(mbuf2,
2931 &action->dscp_table,
2937 drop_mask3 |= pkt_work_mtr(mbuf3,
2939 &action->dscp_table,
2946 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
2948 action_data_get(table_entry0, action, RTE_TABLE_ACTION_TM);
2950 action_data_get(table_entry1, action, RTE_TABLE_ACTION_TM);
2952 action_data_get(table_entry2, action, RTE_TABLE_ACTION_TM);
2954 action_data_get(table_entry3, action, RTE_TABLE_ACTION_TM);
2958 &action->dscp_table,
2963 &action->dscp_table,
2968 &action->dscp_table,
2973 &action->dscp_table,
2977 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
2979 action_data_get(table_entry0, action, RTE_TABLE_ACTION_ENCAP);
2981 action_data_get(table_entry1, action, RTE_TABLE_ACTION_ENCAP);
2983 action_data_get(table_entry2, action, RTE_TABLE_ACTION_ENCAP);
2985 action_data_get(table_entry3, action, RTE_TABLE_ACTION_ENCAP);
2987 pkt_work_encap(mbuf0,
2994 pkt_work_encap(mbuf1,
3001 pkt_work_encap(mbuf2,
3008 pkt_work_encap(mbuf3,
3016 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
3018 action_data_get(table_entry0, action, RTE_TABLE_ACTION_NAT);
3020 action_data_get(table_entry1, action, RTE_TABLE_ACTION_NAT);
3022 action_data_get(table_entry2, action, RTE_TABLE_ACTION_NAT);
3024 action_data_get(table_entry3, action, RTE_TABLE_ACTION_NAT);
3026 if (cfg->common.ip_version) {
3027 pkt_ipv4_work_nat(ip0, data0, &cfg->nat);
3028 pkt_ipv4_work_nat(ip1, data1, &cfg->nat);
3029 pkt_ipv4_work_nat(ip2, data2, &cfg->nat);
3030 pkt_ipv4_work_nat(ip3, data3, &cfg->nat);
3032 pkt_ipv6_work_nat(ip0, data0, &cfg->nat);
3033 pkt_ipv6_work_nat(ip1, data1, &cfg->nat);
3034 pkt_ipv6_work_nat(ip2, data2, &cfg->nat);
3035 pkt_ipv6_work_nat(ip3, data3, &cfg->nat);
3039 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
3041 action_data_get(table_entry0, action, RTE_TABLE_ACTION_TTL);
3043 action_data_get(table_entry1, action, RTE_TABLE_ACTION_TTL);
3045 action_data_get(table_entry2, action, RTE_TABLE_ACTION_TTL);
3047 action_data_get(table_entry3, action, RTE_TABLE_ACTION_TTL);
3049 if (cfg->common.ip_version) {
3050 drop_mask0 |= pkt_ipv4_work_ttl(ip0, data0);
3051 drop_mask1 |= pkt_ipv4_work_ttl(ip1, data1);
3052 drop_mask2 |= pkt_ipv4_work_ttl(ip2, data2);
3053 drop_mask3 |= pkt_ipv4_work_ttl(ip3, data3);
3055 drop_mask0 |= pkt_ipv6_work_ttl(ip0, data0);
3056 drop_mask1 |= pkt_ipv6_work_ttl(ip1, data1);
3057 drop_mask2 |= pkt_ipv6_work_ttl(ip2, data2);
3058 drop_mask3 |= pkt_ipv6_work_ttl(ip3, data3);
3062 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
3064 action_data_get(table_entry0, action, RTE_TABLE_ACTION_STATS);
3066 action_data_get(table_entry1, action, RTE_TABLE_ACTION_STATS);
3068 action_data_get(table_entry2, action, RTE_TABLE_ACTION_STATS);
3070 action_data_get(table_entry3, action, RTE_TABLE_ACTION_STATS);
3072 pkt_work_stats(data0, total_length0);
3073 pkt_work_stats(data1, total_length1);
3074 pkt_work_stats(data2, total_length2);
3075 pkt_work_stats(data3, total_length3);
3078 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
3080 action_data_get(table_entry0, action, RTE_TABLE_ACTION_TIME);
3082 action_data_get(table_entry1, action, RTE_TABLE_ACTION_TIME);
3084 action_data_get(table_entry2, action, RTE_TABLE_ACTION_TIME);
3086 action_data_get(table_entry3, action, RTE_TABLE_ACTION_TIME);
3088 pkt_work_time(data0, time);
3089 pkt_work_time(data1, time);
3090 pkt_work_time(data2, time);
3091 pkt_work_time(data3, time);
3094 if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
3095 void *data0 = action_data_get(table_entry0, action,
3096 RTE_TABLE_ACTION_SYM_CRYPTO);
3097 void *data1 = action_data_get(table_entry1, action,
3098 RTE_TABLE_ACTION_SYM_CRYPTO);
3099 void *data2 = action_data_get(table_entry2, action,
3100 RTE_TABLE_ACTION_SYM_CRYPTO);
3101 void *data3 = action_data_get(table_entry3, action,
3102 RTE_TABLE_ACTION_SYM_CRYPTO);
3104 drop_mask0 |= pkt_work_sym_crypto(mbuf0, data0, &cfg->sym_crypto,
3106 drop_mask1 |= pkt_work_sym_crypto(mbuf1, data1, &cfg->sym_crypto,
3108 drop_mask2 |= pkt_work_sym_crypto(mbuf2, data2, &cfg->sym_crypto,
3110 drop_mask3 |= pkt_work_sym_crypto(mbuf3, data3, &cfg->sym_crypto,
3120 static __rte_always_inline int
3121 ah(struct rte_pipeline *p,
3122 struct rte_mbuf **pkts,
3124 struct rte_pipeline_table_entry **entries,
3125 struct rte_table_action *action,
3126 struct ap_config *cfg)
3128 uint64_t pkts_drop_mask = 0;
3131 if (cfg->action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
3132 (1LLU << RTE_TABLE_ACTION_TIME)))
3135 if ((pkts_mask & (pkts_mask + 1)) == 0) {
3136 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
3139 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {
3142 drop_mask = pkt4_work(&pkts[i],
3148 pkts_drop_mask |= drop_mask << i;
3151 for ( ; i < n_pkts; i++) {
3154 drop_mask = pkt_work(pkts[i],
3160 pkts_drop_mask |= drop_mask << i;
3163 for ( ; pkts_mask; ) {
3164 uint32_t pos = __builtin_ctzll(pkts_mask);
3165 uint64_t pkt_mask = 1LLU << pos;
3168 drop_mask = pkt_work(pkts[pos],
3174 pkts_mask &= ~pkt_mask;
3175 pkts_drop_mask |= drop_mask << pos;
3178 rte_pipeline_ah_packet_drop(p, pkts_drop_mask);
3184 ah_default(struct rte_pipeline *p,
3185 struct rte_mbuf **pkts,
3187 struct rte_pipeline_table_entry **entries,
3190 struct rte_table_action *action = arg;
3200 static rte_pipeline_table_action_handler_hit
3201 ah_selector(struct rte_table_action *action)
3203 if (action->cfg.action_mask == (1LLU << RTE_TABLE_ACTION_FWD))
3210 rte_table_action_table_params_get(struct rte_table_action *action,
3211 struct rte_pipeline_table_params *params)
3213 rte_pipeline_table_action_handler_hit f_action_hit;
3214 uint32_t total_size;
3216 /* Check input arguments */
3217 if ((action == NULL) ||
3221 f_action_hit = ah_selector(action);
3222 total_size = rte_align32pow2(action->data.total_size);
3224 /* Fill in params */
3225 params->f_action_hit = f_action_hit;
3226 params->f_action_miss = NULL;
3227 params->arg_ah = (f_action_hit) ? action : NULL;
3228 params->action_data_size = total_size -
3229 sizeof(struct rte_pipeline_table_entry);
3235 rte_table_action_free(struct rte_table_action *action)