4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/queue.h>
42 #include <rte_ether.h>
43 #include <rte_ethdev.h>
45 #include <rte_memzone.h>
46 #include <rte_malloc.h>
52 #include "i40e_logs.h"
53 #include "i40e/i40e_type.h"
54 #include "i40e_ethdev.h"
55 #include "i40e_rxtx.h"
57 #define I40E_FDIR_MZ_NAME "FDIR_MEMZONE"
59 #define IPV6_ADDR_LEN 16
62 #define I40E_FDIR_PKT_LEN 512
63 #define I40E_FDIR_IP_DEFAULT_LEN 420
64 #define I40E_FDIR_IP_DEFAULT_TTL 0x40
65 #define I40E_FDIR_IP_DEFAULT_VERSION_IHL 0x45
66 #define I40E_FDIR_TCP_DEFAULT_DATAOFF 0x50
67 #define I40E_FDIR_IPv6_DEFAULT_VTC_FLOW 0x60300000
68 #define I40E_FDIR_IPv6_DEFAULT_HOP_LIMITS 0xFF
69 #define I40E_FDIR_IPv6_PAYLOAD_LEN 380
70 #define I40E_FDIR_UDP_DEFAULT_LEN 400
72 /* Wait count and interval for fdir filter programming */
73 #define I40E_FDIR_WAIT_COUNT 10
74 #define I40E_FDIR_WAIT_INTERVAL_US 1000
76 /* Wait count and interval for fdir filter flush */
77 #define I40E_FDIR_FLUSH_RETRY 50
78 #define I40E_FDIR_FLUSH_INTERVAL_MS 5
80 #define I40E_COUNTER_PF 2
81 /* Statistic counter index for one pf */
82 #define I40E_COUNTER_INDEX_FDIR(pf_id) (0 + (pf_id) * I40E_COUNTER_PF)
83 #define I40E_MAX_FLX_SOURCE_OFF 480
84 #define I40E_FLX_OFFSET_IN_FIELD_VECTOR 50
86 #define NONUSE_FLX_PIT_DEST_OFF 63
87 #define NONUSE_FLX_PIT_FSIZE 1
88 #define MK_FLX_PIT(src_offset, fsize, dst_offset) ( \
89 (((src_offset) << I40E_PRTQF_FLX_PIT_SOURCE_OFF_SHIFT) & \
90 I40E_PRTQF_FLX_PIT_SOURCE_OFF_MASK) | \
91 (((fsize) << I40E_PRTQF_FLX_PIT_FSIZE_SHIFT) & \
92 I40E_PRTQF_FLX_PIT_FSIZE_MASK) | \
93 ((((dst_offset) + I40E_FLX_OFFSET_IN_FIELD_VECTOR) << \
94 I40E_PRTQF_FLX_PIT_DEST_OFF_SHIFT) & \
95 I40E_PRTQF_FLX_PIT_DEST_OFF_MASK))
97 #define I40E_FDIR_FLOW_TYPES ( \
98 (1 << RTE_ETH_FLOW_TYPE_UDPV4) | \
99 (1 << RTE_ETH_FLOW_TYPE_TCPV4) | \
100 (1 << RTE_ETH_FLOW_TYPE_SCTPV4) | \
101 (1 << RTE_ETH_FLOW_TYPE_IPV4_OTHER) | \
102 (1 << RTE_ETH_FLOW_TYPE_FRAG_IPV4) | \
103 (1 << RTE_ETH_FLOW_TYPE_UDPV6) | \
104 (1 << RTE_ETH_FLOW_TYPE_TCPV6) | \
105 (1 << RTE_ETH_FLOW_TYPE_SCTPV6) | \
106 (1 << RTE_ETH_FLOW_TYPE_IPV6_OTHER) | \
107 (1 << RTE_ETH_FLOW_TYPE_FRAG_IPV6))
109 #define I40E_FLEX_WORD_MASK(off) (0x80 >> (off))
111 static int i40e_fdir_rx_queue_init(struct i40e_rx_queue *rxq);
112 static int i40e_fdir_construct_pkt(struct i40e_pf *pf,
113 const struct rte_eth_fdir_input *fdir_input,
114 unsigned char *raw_pkt);
115 static int i40e_add_del_fdir_filter(struct rte_eth_dev *dev,
116 const struct rte_eth_fdir_filter *filter,
118 static int i40e_fdir_filter_programming(struct i40e_pf *pf,
119 enum i40e_filter_pctype pctype,
120 const struct rte_eth_fdir_filter *filter,
122 static int i40e_fdir_flush(struct rte_eth_dev *dev);
123 static void i40e_fdir_info_get(struct rte_eth_dev *dev,
124 struct rte_eth_fdir_info *fdir);
125 static void i40e_fdir_stats_get(struct rte_eth_dev *dev,
126 struct rte_eth_fdir_stats *stat);
129 i40e_fdir_rx_queue_init(struct i40e_rx_queue *rxq)
131 struct i40e_hw *hw = I40E_VSI_TO_HW(rxq->vsi);
132 struct i40e_hmc_obj_rxq rx_ctx;
133 int err = I40E_SUCCESS;
135 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
136 /* Init the RX queue in hardware */
137 rx_ctx.dbuff = I40E_RXBUF_SZ_1024 >> I40E_RXQ_CTX_DBUFF_SHIFT;
139 rx_ctx.base = rxq->rx_ring_phys_addr / I40E_QUEUE_BASE_ADDR_UNIT;
140 rx_ctx.qlen = rxq->nb_rx_desc;
141 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
144 rx_ctx.dtype = i40e_header_split_none;
145 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_NONE;
146 rx_ctx.rxmax = ETHER_MAX_LEN;
147 rx_ctx.tphrdesc_ena = 1;
148 rx_ctx.tphwdesc_ena = 1;
149 rx_ctx.tphdata_ena = 1;
150 rx_ctx.tphhead_ena = 1;
151 rx_ctx.lrxqthresh = 2;
157 err = i40e_clear_lan_rx_queue_context(hw, rxq->reg_idx);
158 if (err != I40E_SUCCESS) {
159 PMD_DRV_LOG(ERR, "Failed to clear FDIR RX queue context.");
162 err = i40e_set_lan_rx_queue_context(hw, rxq->reg_idx, &rx_ctx);
163 if (err != I40E_SUCCESS) {
164 PMD_DRV_LOG(ERR, "Failed to set FDIR RX queue context.");
167 rxq->qrx_tail = hw->hw_addr +
168 I40E_QRX_TAIL(rxq->vsi->base_queue);
171 /* Init the RX tail regieter. */
172 I40E_PCI_REG_WRITE(rxq->qrx_tail, 0);
173 I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
179 * i40e_fdir_setup - reserve and initialize the Flow Director resources
180 * @pf: board private structure
183 i40e_fdir_setup(struct i40e_pf *pf)
185 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
186 struct i40e_vsi *vsi;
187 int err = I40E_SUCCESS;
188 char z_name[RTE_MEMZONE_NAMESIZE];
189 const struct rte_memzone *mz = NULL;
190 struct rte_eth_dev *eth_dev = pf->adapter->eth_dev;
192 PMD_DRV_LOG(INFO, "FDIR HW Capabilities: num_filters_guaranteed = %u,"
193 " num_filters_best_effort = %u.",
194 hw->func_caps.fd_filters_guaranteed,
195 hw->func_caps.fd_filters_best_effort);
197 vsi = pf->fdir.fdir_vsi;
199 PMD_DRV_LOG(ERR, "FDIR vsi pointer needs "
200 "to be null before creation.");
201 return I40E_ERR_BAD_PTR;
203 /* make new FDIR VSI */
204 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->main_vsi, 0);
206 PMD_DRV_LOG(ERR, "Couldn't create FDIR VSI.");
207 return I40E_ERR_NO_AVAILABLE_VSI;
209 pf->fdir.fdir_vsi = vsi;
211 /*Fdir tx queue setup*/
212 err = i40e_fdir_setup_tx_resources(pf);
214 PMD_DRV_LOG(ERR, "Failed to setup FDIR TX resources.");
218 /*Fdir rx queue setup*/
219 err = i40e_fdir_setup_rx_resources(pf);
221 PMD_DRV_LOG(ERR, "Failed to setup FDIR RX resources.");
225 err = i40e_tx_queue_init(pf->fdir.txq);
227 PMD_DRV_LOG(ERR, "Failed to do FDIR TX initialization.");
231 /* need switch on before dev start*/
232 err = i40e_switch_tx_queue(hw, vsi->base_queue, TRUE);
234 PMD_DRV_LOG(ERR, "Failed to do fdir TX switch on.");
238 /* Init the rx queue in hardware */
239 err = i40e_fdir_rx_queue_init(pf->fdir.rxq);
241 PMD_DRV_LOG(ERR, "Failed to do FDIR RX initialization.");
245 /* switch on rx queue */
246 err = i40e_switch_rx_queue(hw, vsi->base_queue, TRUE);
248 PMD_DRV_LOG(ERR, "Failed to do FDIR RX switch on.");
252 /* reserve memory for the fdir programming packet */
253 snprintf(z_name, sizeof(z_name), "%s_%s_%d",
254 eth_dev->driver->pci_drv.name,
256 eth_dev->data->port_id);
257 mz = i40e_memzone_reserve(z_name, I40E_FDIR_PKT_LEN, SOCKET_ID_ANY);
259 PMD_DRV_LOG(ERR, "Cannot init memzone for "
260 "flow director program packet.");
261 err = I40E_ERR_NO_MEMORY;
264 pf->fdir.prg_pkt = mz->addr;
265 #ifdef RTE_LIBRTE_XEN_DOM0
266 pf->fdir.dma_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
268 pf->fdir.dma_addr = (uint64_t)mz->phys_addr;
270 pf->fdir.match_counter_index = I40E_COUNTER_INDEX_FDIR(hw->pf_id);
271 PMD_DRV_LOG(INFO, "FDIR setup successfully, with programming queue %u.",
276 i40e_dev_rx_queue_release(pf->fdir.rxq);
279 i40e_dev_tx_queue_release(pf->fdir.txq);
282 i40e_vsi_release(vsi);
283 pf->fdir.fdir_vsi = NULL;
288 * i40e_fdir_teardown - release the Flow Director resources
289 * @pf: board private structure
292 i40e_fdir_teardown(struct i40e_pf *pf)
294 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
295 struct i40e_vsi *vsi;
297 vsi = pf->fdir.fdir_vsi;
298 i40e_switch_tx_queue(hw, vsi->base_queue, FALSE);
299 i40e_switch_rx_queue(hw, vsi->base_queue, FALSE);
300 i40e_dev_rx_queue_release(pf->fdir.rxq);
302 i40e_dev_tx_queue_release(pf->fdir.txq);
304 i40e_vsi_release(vsi);
305 pf->fdir.fdir_vsi = NULL;
308 /* check whether the flow director table in empty */
310 i40e_fdir_empty(struct i40e_hw *hw)
312 uint32_t guarant_cnt, best_cnt;
314 guarant_cnt = (uint32_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
315 I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) >>
316 I40E_PFQF_FDSTAT_GUARANT_CNT_SHIFT);
317 best_cnt = (uint32_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
318 I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
319 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
320 if (best_cnt + guarant_cnt > 0)
327 * Initialize the configuration about bytes stream extracted as flexible payload
331 i40e_init_flx_pld(struct i40e_pf *pf)
333 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
338 * Define the bytes stream extracted as flexible payload in
339 * field vector. By default, select 8 words from the beginning
340 * of payload as flexible payload.
342 for (i = I40E_FLXPLD_L2_IDX; i < I40E_MAX_FLXPLD_LAYER; i++) {
343 index = i * I40E_MAX_FLXPLD_FIED;
344 pf->fdir.flex_set[index].src_offset = 0;
345 pf->fdir.flex_set[index].size = I40E_FDIR_MAX_FLEXWORD_NUM;
346 pf->fdir.flex_set[index].dst_offset = 0;
347 I40E_WRITE_REG(hw, I40E_PRTQF_FLX_PIT(index), 0x0000C900);
349 I40E_PRTQF_FLX_PIT(index + 1), 0x0000FC29);/*non-used*/
351 I40E_PRTQF_FLX_PIT(index + 2), 0x0000FC2A);/*non-used*/
354 /* initialize the masks */
355 for (pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
356 pctype <= I40E_FILTER_PCTYPE_FRAG_IPV6; pctype++) {
357 pf->fdir.flex_mask[pctype].word_mask = 0;
358 I40E_WRITE_REG(hw, I40E_PRTQF_FD_FLXINSET(pctype), 0);
359 for (i = 0; i < I40E_FDIR_BITMASK_NUM_WORD; i++) {
360 pf->fdir.flex_mask[pctype].bitmask[i].offset = 0;
361 pf->fdir.flex_mask[pctype].bitmask[i].mask = 0;
362 I40E_WRITE_REG(hw, I40E_PRTQF_FD_MSK(pctype, i), 0);
368 * Configure flow director related setting
371 i40e_fdir_configure(struct rte_eth_dev *dev)
373 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
374 struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
379 * configuration need to be done before
380 * flow director filters are added
381 * If filters exist, flush them.
383 if (i40e_fdir_empty(hw) < 0) {
384 ret = i40e_fdir_flush(dev);
386 PMD_DRV_LOG(ERR, "failed to flush fdir table.");
391 val = I40E_READ_REG(hw, I40E_PFQF_CTL_0);
392 if ((pf->flags & I40E_FLAG_FDIR) &&
393 dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT) {
394 /* enable FDIR filter */
395 val |= I40E_PFQF_CTL_0_FD_ENA_MASK;
396 I40E_WRITE_REG(hw, I40E_PFQF_CTL_0, val);
398 i40e_init_flx_pld(pf); /* set flex config to default value */
400 /* disable FDIR filter */
401 val &= ~I40E_PFQF_CTL_0_FD_ENA_MASK;
402 I40E_WRITE_REG(hw, I40E_PFQF_CTL_0, val);
403 pf->flags &= ~I40E_FLAG_FDIR;
410 i40e_fdir_fill_eth_ip_head(const struct rte_eth_fdir_input *fdir_input,
411 unsigned char *raw_pkt)
413 struct ether_hdr *ether = (struct ether_hdr *)raw_pkt;
415 struct ipv6_hdr *ip6;
416 static const uint8_t next_proto[] = {
417 [RTE_ETH_FLOW_TYPE_UDPV4] = IPPROTO_UDP,
418 [RTE_ETH_FLOW_TYPE_TCPV4] = IPPROTO_TCP,
419 [RTE_ETH_FLOW_TYPE_SCTPV4] = IPPROTO_SCTP,
420 [RTE_ETH_FLOW_TYPE_IPV4_OTHER] = IPPROTO_IP,
421 [RTE_ETH_FLOW_TYPE_FRAG_IPV4] = IPPROTO_IP,
422 [RTE_ETH_FLOW_TYPE_UDPV6] = IPPROTO_UDP,
423 [RTE_ETH_FLOW_TYPE_TCPV6] = IPPROTO_TCP,
424 [RTE_ETH_FLOW_TYPE_SCTPV6] = IPPROTO_SCTP,
425 [RTE_ETH_FLOW_TYPE_IPV6_OTHER] = IPPROTO_NONE,
426 [RTE_ETH_FLOW_TYPE_FRAG_IPV6] = IPPROTO_NONE,
429 switch (fdir_input->flow_type) {
430 case RTE_ETH_FLOW_TYPE_UDPV4:
431 case RTE_ETH_FLOW_TYPE_TCPV4:
432 case RTE_ETH_FLOW_TYPE_SCTPV4:
433 case RTE_ETH_FLOW_TYPE_IPV4_OTHER:
434 case RTE_ETH_FLOW_TYPE_FRAG_IPV4:
435 ip = (struct ipv4_hdr *)(raw_pkt + sizeof(struct ether_hdr));
437 ether->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
438 ip->version_ihl = I40E_FDIR_IP_DEFAULT_VERSION_IHL;
439 /* set len to by default */
440 ip->total_length = rte_cpu_to_be_16(I40E_FDIR_IP_DEFAULT_LEN);
441 ip->time_to_live = I40E_FDIR_IP_DEFAULT_TTL;
443 * The source and destination fields in the transmitted packet
444 * need to be presented in a reversed order with respect
445 * to the expected received packets.
447 ip->src_addr = fdir_input->flow.ip4_flow.dst_ip;
448 ip->dst_addr = fdir_input->flow.ip4_flow.src_ip;
449 ip->next_proto_id = next_proto[fdir_input->flow_type];
451 case RTE_ETH_FLOW_TYPE_UDPV6:
452 case RTE_ETH_FLOW_TYPE_TCPV6:
453 case RTE_ETH_FLOW_TYPE_SCTPV6:
454 case RTE_ETH_FLOW_TYPE_IPV6_OTHER:
455 case RTE_ETH_FLOW_TYPE_FRAG_IPV6:
456 ip6 = (struct ipv6_hdr *)(raw_pkt + sizeof(struct ether_hdr));
458 ether->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
460 rte_cpu_to_be_32(I40E_FDIR_IPv6_DEFAULT_VTC_FLOW);
462 rte_cpu_to_be_16(I40E_FDIR_IPv6_PAYLOAD_LEN);
463 ip6->hop_limits = I40E_FDIR_IPv6_DEFAULT_HOP_LIMITS;
466 * The source and destination fields in the transmitted packet
467 * need to be presented in a reversed order with respect
468 * to the expected received packets.
470 rte_memcpy(&(ip6->src_addr),
471 &(fdir_input->flow.ip6_flow.dst_ip),
473 rte_memcpy(&(ip6->dst_addr),
474 &(fdir_input->flow.ip6_flow.src_ip),
476 ip6->proto = next_proto[fdir_input->flow_type];
479 PMD_DRV_LOG(ERR, "unknown flow type %u.",
480 fdir_input->flow_type);
487 * i40e_fdir_construct_pkt - construct packet based on fields in input
488 * @pf: board private structure
489 * @fdir_input: input set of the flow director entry
490 * @raw_pkt: a packet to be constructed
493 i40e_fdir_construct_pkt(struct i40e_pf *pf,
494 const struct rte_eth_fdir_input *fdir_input,
495 unsigned char *raw_pkt)
497 unsigned char *payload, *ptr;
500 struct sctp_hdr *sctp;
501 uint8_t size, dst = 0;
502 uint8_t i, pit_idx, set_idx = I40E_FLXPLD_L4_IDX; /* use l4 by default*/
504 /* fill the ethernet and IP head */
505 i40e_fdir_fill_eth_ip_head(fdir_input, raw_pkt);
507 /* fill the L4 head */
508 switch (fdir_input->flow_type) {
509 case RTE_ETH_FLOW_TYPE_UDPV4:
510 udp = (struct udp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
511 sizeof(struct ipv4_hdr));
512 payload = (unsigned char *)udp + sizeof(struct udp_hdr);
514 * The source and destination fields in the transmitted packet
515 * need to be presented in a reversed order with respect
516 * to the expected received packets.
518 udp->src_port = fdir_input->flow.udp4_flow.dst_port;
519 udp->dst_port = fdir_input->flow.udp4_flow.src_port;
520 udp->dgram_len = rte_cpu_to_be_16(I40E_FDIR_UDP_DEFAULT_LEN);
523 case RTE_ETH_FLOW_TYPE_TCPV4:
524 tcp = (struct tcp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
525 sizeof(struct ipv4_hdr));
526 payload = (unsigned char *)tcp + sizeof(struct tcp_hdr);
528 * The source and destination fields in the transmitted packet
529 * need to be presented in a reversed order with respect
530 * to the expected received packets.
532 tcp->src_port = fdir_input->flow.tcp4_flow.dst_port;
533 tcp->dst_port = fdir_input->flow.tcp4_flow.src_port;
534 tcp->data_off = I40E_FDIR_TCP_DEFAULT_DATAOFF;
537 case RTE_ETH_FLOW_TYPE_SCTPV4:
538 sctp = (struct sctp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
539 sizeof(struct ipv4_hdr));
540 payload = (unsigned char *)sctp + sizeof(struct sctp_hdr);
541 sctp->tag = fdir_input->flow.sctp4_flow.verify_tag;
544 case RTE_ETH_FLOW_TYPE_IPV4_OTHER:
545 case RTE_ETH_FLOW_TYPE_FRAG_IPV4:
546 payload = raw_pkt + sizeof(struct ether_hdr) +
547 sizeof(struct ipv4_hdr);
548 set_idx = I40E_FLXPLD_L3_IDX;
551 case RTE_ETH_FLOW_TYPE_UDPV6:
552 udp = (struct udp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
553 sizeof(struct ipv6_hdr));
554 payload = (unsigned char *)udp + sizeof(struct udp_hdr);
556 * The source and destination fields in the transmitted packet
557 * need to be presented in a reversed order with respect
558 * to the expected received packets.
560 udp->src_port = fdir_input->flow.udp6_flow.dst_port;
561 udp->dst_port = fdir_input->flow.udp6_flow.src_port;
562 udp->dgram_len = rte_cpu_to_be_16(I40E_FDIR_IPv6_PAYLOAD_LEN);
565 case RTE_ETH_FLOW_TYPE_TCPV6:
566 tcp = (struct tcp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
567 sizeof(struct ipv6_hdr));
568 payload = (unsigned char *)tcp + sizeof(struct tcp_hdr);
570 * The source and destination fields in the transmitted packet
571 * need to be presented in a reversed order with respect
572 * to the expected received packets.
574 tcp->data_off = I40E_FDIR_TCP_DEFAULT_DATAOFF;
575 tcp->src_port = fdir_input->flow.udp6_flow.dst_port;
576 tcp->dst_port = fdir_input->flow.udp6_flow.src_port;
579 case RTE_ETH_FLOW_TYPE_SCTPV6:
580 sctp = (struct sctp_hdr *)(raw_pkt + sizeof(struct ether_hdr) +
581 sizeof(struct ipv6_hdr));
582 payload = (unsigned char *)sctp + sizeof(struct sctp_hdr);
583 sctp->tag = fdir_input->flow.sctp6_flow.verify_tag;
586 case RTE_ETH_FLOW_TYPE_IPV6_OTHER:
587 case RTE_ETH_FLOW_TYPE_FRAG_IPV6:
588 payload = raw_pkt + sizeof(struct ether_hdr) +
589 sizeof(struct ipv6_hdr);
590 set_idx = I40E_FLXPLD_L3_IDX;
593 PMD_DRV_LOG(ERR, "unknown flow type %u.", fdir_input->flow_type);
597 /* fill the flexbytes to payload */
598 for (i = 0; i < I40E_MAX_FLXPLD_FIED; i++) {
599 pit_idx = set_idx * I40E_MAX_FLXPLD_FIED + i;
600 size = pf->fdir.flex_set[pit_idx].size;
603 dst = pf->fdir.flex_set[pit_idx].dst_offset * sizeof(uint16_t);
605 pf->fdir.flex_set[pit_idx].src_offset * sizeof(uint16_t);
606 (void)rte_memcpy(ptr,
607 &fdir_input->flow_ext.flexbytes[dst],
608 size * sizeof(uint16_t));
614 /* Construct the tx flags */
615 static inline uint64_t
616 i40e_build_ctob(uint32_t td_cmd,
621 return rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DATA |
622 ((uint64_t)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
623 ((uint64_t)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
624 ((uint64_t)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
625 ((uint64_t)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
629 * check the programming status descriptor in rx queue.
630 * done after Programming Flow Director is programmed on
634 i40e_check_fdir_programming_status(struct i40e_rx_queue *rxq)
636 volatile union i40e_rx_desc *rxdp;
643 rxdp = &rxq->rx_ring[rxq->rx_tail];
644 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
645 rx_status = (qword1 & I40E_RXD_QW1_STATUS_MASK)
646 >> I40E_RXD_QW1_STATUS_SHIFT;
648 if (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
649 len = qword1 >> I40E_RX_PROG_STATUS_DESC_LENGTH_SHIFT;
650 id = (qword1 & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
651 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
653 if (len == I40E_RX_PROG_STATUS_DESC_LENGTH &&
654 id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS) {
656 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
657 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
659 I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
660 PMD_DRV_LOG(ERR, "Failed to add FDIR filter"
661 " (FD_ID %u): programming status"
663 rxdp->wb.qword0.hi_dword.fd_id);
665 } else if (error == (0x1 <<
666 I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
667 PMD_DRV_LOG(ERR, "Failed to delete FDIR filter"
668 " (FD_ID %u): programming status"
670 rxdp->wb.qword0.hi_dword.fd_id);
673 PMD_DRV_LOG(ERR, "invalid programming status"
674 " reported, error = %u.", error);
676 PMD_DRV_LOG(ERR, "unknown programming status"
677 " reported, len = %d, id = %u.", len, id);
678 rxdp->wb.qword1.status_error_len = 0;
680 if (unlikely(rxq->rx_tail == rxq->nb_rx_desc))
687 * i40e_add_del_fdir_filter - add or remove a flow director filter.
688 * @pf: board private structure
689 * @filter: fdir filter entry
690 * @add: 0 - delete, 1 - add
693 i40e_add_del_fdir_filter(struct rte_eth_dev *dev,
694 const struct rte_eth_fdir_filter *filter,
697 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
698 unsigned char *pkt = (unsigned char *)pf->fdir.prg_pkt;
699 enum i40e_filter_pctype pctype;
702 if (!(pf->flags & I40E_FLAG_FDIR)) {
703 PMD_DRV_LOG(ERR, "FDIR is not enabled.");
706 if (!I40E_VALID_FLOW_TYPE(filter->input.flow_type)) {
707 PMD_DRV_LOG(ERR, "invalid flow_type input.");
710 if (filter->action.rx_queue >= pf->dev_data->nb_rx_queues) {
711 PMD_DRV_LOG(ERR, "Invalid queue ID");
715 memset(pkt, 0, I40E_FDIR_PKT_LEN);
717 ret = i40e_fdir_construct_pkt(pf, &filter->input, pkt);
719 PMD_DRV_LOG(ERR, "construct packet for fdir fails.");
722 pctype = i40e_flowtype_to_pctype(filter->input.flow_type);
723 ret = i40e_fdir_filter_programming(pf, pctype, filter, add);
725 PMD_DRV_LOG(ERR, "fdir programming fails for PCTYPE(%u).",
733 * i40e_fdir_filter_programming - Program a flow director filter rule.
734 * Is done by Flow Director Programming Descriptor followed by packet
735 * structure that contains the filter fields need to match.
736 * @pf: board private structure
738 * @filter: fdir filter entry
739 * @add: 0 - delelet, 1 - add
742 i40e_fdir_filter_programming(struct i40e_pf *pf,
743 enum i40e_filter_pctype pctype,
744 const struct rte_eth_fdir_filter *filter,
747 struct i40e_tx_queue *txq = pf->fdir.txq;
748 struct i40e_rx_queue *rxq = pf->fdir.rxq;
749 const struct rte_eth_fdir_action *fdir_action = &filter->action;
750 volatile struct i40e_tx_desc *txdp;
751 volatile struct i40e_filter_program_desc *fdirdp;
756 PMD_DRV_LOG(INFO, "filling filter programming descriptor.");
757 fdirdp = (volatile struct i40e_filter_program_desc *)
758 (&(txq->tx_ring[txq->tx_tail]));
760 fdirdp->qindex_flex_ptype_vsi =
761 rte_cpu_to_le_32((fdir_action->rx_queue <<
762 I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
763 I40E_TXD_FLTR_QW0_QINDEX_MASK);
765 fdirdp->qindex_flex_ptype_vsi |=
766 rte_cpu_to_le_32((fdir_action->flex_off <<
767 I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
768 I40E_TXD_FLTR_QW0_FLEXOFF_MASK);
770 fdirdp->qindex_flex_ptype_vsi |=
771 rte_cpu_to_le_32((pctype <<
772 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
773 I40E_TXD_FLTR_QW0_PCTYPE_MASK);
775 /* Use LAN VSI Id by default */
776 fdirdp->qindex_flex_ptype_vsi |=
777 rte_cpu_to_le_32((pf->main_vsi->vsi_id <<
778 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
779 I40E_TXD_FLTR_QW0_DEST_VSI_MASK);
781 fdirdp->dtype_cmd_cntindex =
782 rte_cpu_to_le_32(I40E_TX_DESC_DTYPE_FILTER_PROG);
785 fdirdp->dtype_cmd_cntindex |= rte_cpu_to_le_32(
786 I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
787 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
789 fdirdp->dtype_cmd_cntindex |= rte_cpu_to_le_32(
790 I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
791 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
793 if (fdir_action->behavior == RTE_ETH_FDIR_REJECT)
794 dest = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
796 dest = I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
797 fdirdp->dtype_cmd_cntindex |= rte_cpu_to_le_32((dest <<
798 I40E_TXD_FLTR_QW1_DEST_SHIFT) &
799 I40E_TXD_FLTR_QW1_DEST_MASK);
801 fdirdp->dtype_cmd_cntindex |=
802 rte_cpu_to_le_32((fdir_action->report_status<<
803 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
804 I40E_TXD_FLTR_QW1_FD_STATUS_MASK);
806 fdirdp->dtype_cmd_cntindex |=
807 rte_cpu_to_le_32(I40E_TXD_FLTR_QW1_CNT_ENA_MASK);
808 fdirdp->dtype_cmd_cntindex |=
809 rte_cpu_to_le_32((pf->fdir.match_counter_index <<
810 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
811 I40E_TXD_FLTR_QW1_CNTINDEX_MASK);
813 fdirdp->fd_id = rte_cpu_to_le_32(filter->soft_id);
815 PMD_DRV_LOG(INFO, "filling transmit descriptor.");
816 txdp = &(txq->tx_ring[txq->tx_tail + 1]);
817 txdp->buffer_addr = rte_cpu_to_le_64(pf->fdir.dma_addr);
818 td_cmd = I40E_TX_DESC_CMD_EOP |
819 I40E_TX_DESC_CMD_RS |
820 I40E_TX_DESC_CMD_DUMMY;
822 txdp->cmd_type_offset_bsz =
823 i40e_build_ctob(td_cmd, 0, I40E_FDIR_PKT_LEN, 0);
825 txq->tx_tail += 2; /* set 2 descriptors above, fdirdp and txdp */
826 if (txq->tx_tail >= txq->nb_tx_desc)
828 /* Update the tx tail register */
830 I40E_PCI_REG_WRITE(txq->qtx_tail, txq->tx_tail);
832 for (i = 0; i < I40E_FDIR_WAIT_COUNT; i++) {
833 rte_delay_us(I40E_FDIR_WAIT_INTERVAL_US);
834 if (txdp->cmd_type_offset_bsz &
835 rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DESC_DONE))
838 if (i >= I40E_FDIR_WAIT_COUNT) {
839 PMD_DRV_LOG(ERR, "Failed to program FDIR filter:"
840 " time out to get DD on tx queue.");
843 /* totally delay 10 ms to check programming status*/
844 rte_delay_us((I40E_FDIR_WAIT_COUNT - i) * I40E_FDIR_WAIT_INTERVAL_US);
845 if (i40e_check_fdir_programming_status(rxq) < 0) {
846 PMD_DRV_LOG(ERR, "Failed to program FDIR filter:"
847 " programming status reported.");
855 * i40e_fdir_flush - clear all filters of Flow Director table
856 * @pf: board private structure
859 i40e_fdir_flush(struct rte_eth_dev *dev)
861 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
862 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
864 uint16_t guarant_cnt, best_cnt;
867 I40E_WRITE_REG(hw, I40E_PFQF_CTL_1, I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
868 I40E_WRITE_FLUSH(hw);
870 for (i = 0; i < I40E_FDIR_FLUSH_RETRY; i++) {
871 rte_delay_ms(I40E_FDIR_FLUSH_INTERVAL_MS);
872 reg = I40E_READ_REG(hw, I40E_PFQF_CTL_1);
873 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
876 if (i >= I40E_FDIR_FLUSH_RETRY) {
877 PMD_DRV_LOG(ERR, "FD table did not flush, may need more time.");
880 guarant_cnt = (uint16_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
881 I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) >>
882 I40E_PFQF_FDSTAT_GUARANT_CNT_SHIFT);
883 best_cnt = (uint16_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
884 I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
885 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
886 if (guarant_cnt != 0 || best_cnt != 0) {
887 PMD_DRV_LOG(ERR, "Failed to flush FD table.");
890 PMD_DRV_LOG(INFO, "FD table Flush success.");
895 i40e_fdir_info_get_flex_set(struct i40e_pf *pf,
896 struct rte_eth_flex_payload_cfg *flex_set,
899 struct i40e_fdir_flex_pit *flex_pit;
900 struct rte_eth_flex_payload_cfg *ptr = flex_set;
901 uint16_t src, dst, size, j, k;
902 uint8_t i, layer_idx;
904 for (layer_idx = I40E_FLXPLD_L2_IDX;
905 layer_idx <= I40E_FLXPLD_L4_IDX;
907 if (layer_idx == I40E_FLXPLD_L2_IDX)
908 ptr->type = RTE_ETH_L2_PAYLOAD;
909 else if (layer_idx == I40E_FLXPLD_L3_IDX)
910 ptr->type = RTE_ETH_L3_PAYLOAD;
911 else if (layer_idx == I40E_FLXPLD_L4_IDX)
912 ptr->type = RTE_ETH_L4_PAYLOAD;
914 for (i = 0; i < I40E_MAX_FLXPLD_FIED; i++) {
915 flex_pit = &pf->fdir.flex_set[layer_idx *
916 I40E_MAX_FLXPLD_FIED + i];
917 if (flex_pit->size == 0)
919 src = flex_pit->src_offset * sizeof(uint16_t);
920 dst = flex_pit->dst_offset * sizeof(uint16_t);
921 size = flex_pit->size * sizeof(uint16_t);
922 for (j = src, k = dst; j < src + size; j++, k++)
923 ptr->src_offset[k] = j;
931 i40e_fdir_info_get_flex_mask(struct i40e_pf *pf,
932 struct rte_eth_fdir_flex_mask *flex_mask,
935 struct i40e_fdir_flex_mask *mask;
936 struct rte_eth_fdir_flex_mask *ptr = flex_mask;
937 enum rte_eth_flow_type flow_type;
939 uint16_t off_bytes, mask_tmp;
941 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
942 i <= I40E_FILTER_PCTYPE_FRAG_IPV6;
944 mask = &pf->fdir.flex_mask[i];
945 if (!I40E_VALID_PCTYPE((enum i40e_filter_pctype)i))
947 flow_type = i40e_pctype_to_flowtype((enum i40e_filter_pctype)i);
948 for (j = 0; j < I40E_FDIR_MAX_FLEXWORD_NUM; j++) {
949 if (mask->word_mask & I40E_FLEX_WORD_MASK(j)) {
950 ptr->mask[j * sizeof(uint16_t)] = UINT8_MAX;
951 ptr->mask[j * sizeof(uint16_t) + 1] = UINT8_MAX;
953 ptr->mask[j * sizeof(uint16_t)] = 0x0;
954 ptr->mask[j * sizeof(uint16_t) + 1] = 0x0;
957 for (j = 0; j < I40E_FDIR_BITMASK_NUM_WORD; j++) {
958 off_bytes = mask->bitmask[j].offset * sizeof(uint16_t);
959 mask_tmp = ~mask->bitmask[j].mask;
960 ptr->mask[off_bytes] &= I40E_HI_BYTE(mask_tmp);
961 ptr->mask[off_bytes + 1] &= I40E_LO_BYTE(mask_tmp);
963 ptr->flow_type = flow_type;
970 * i40e_fdir_info_get - get information of Flow Director
971 * @pf: ethernet device to get info from
972 * @fdir: a pointer to a structure of type *rte_eth_fdir_info* to be filled with
973 * the flow director information.
976 i40e_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir)
978 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
979 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
980 uint16_t num_flex_set = 0;
981 uint16_t num_flex_mask = 0;
983 fdir->mode = (pf->flags & I40E_FLAG_FDIR) ?
984 RTE_FDIR_MODE_PERFECT : RTE_FDIR_MODE_NONE;
986 (uint32_t)hw->func_caps.fd_filters_guaranteed;
988 (uint32_t)hw->func_caps.fd_filters_best_effort;
989 fdir->max_flexpayload = I40E_FDIR_MAX_FLEX_LEN;
990 fdir->flow_types_mask[0] = I40E_FDIR_FLOW_TYPES;
991 fdir->flex_payload_unit = sizeof(uint16_t);
992 fdir->flex_bitmask_unit = sizeof(uint16_t);
993 fdir->max_flex_payload_segment_num = I40E_MAX_FLXPLD_FIED;
994 fdir->flex_payload_limit = I40E_MAX_FLX_SOURCE_OFF;
995 fdir->max_flex_bitmask_num = I40E_FDIR_BITMASK_NUM_WORD;
997 i40e_fdir_info_get_flex_set(pf,
998 fdir->flex_conf.flex_set,
1000 i40e_fdir_info_get_flex_mask(pf,
1001 fdir->flex_conf.flex_mask,
1004 fdir->flex_conf.nb_payloads = num_flex_set;
1005 fdir->flex_conf.nb_flexmasks = num_flex_mask;
1009 * i40e_fdir_stat_get - get statistics of Flow Director
1010 * @pf: ethernet device to get info from
1011 * @stat: a pointer to a structure of type *rte_eth_fdir_stats* to be filled with
1012 * the flow director statistics.
1015 i40e_fdir_stats_get(struct rte_eth_dev *dev, struct rte_eth_fdir_stats *stat)
1017 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1018 struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1021 fdstat = I40E_READ_REG(hw, I40E_PFQF_FDSTAT);
1023 (uint32_t)((fdstat & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) >>
1024 I40E_PFQF_FDSTAT_GUARANT_CNT_SHIFT);
1026 (uint32_t)((fdstat & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
1027 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
1031 * i40e_fdir_ctrl_func - deal with all operations on flow director.
1032 * @pf: board private structure
1033 * @filter_op:operation will be taken.
1034 * @arg: a pointer to specific structure corresponding to the filter_op
1037 i40e_fdir_ctrl_func(struct rte_eth_dev *dev,
1038 enum rte_filter_op filter_op,
1041 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1044 if (filter_op == RTE_ETH_FILTER_NOP) {
1045 if (!(pf->flags & I40E_FLAG_FDIR))
1050 if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
1053 switch (filter_op) {
1054 case RTE_ETH_FILTER_ADD:
1055 ret = i40e_add_del_fdir_filter(dev,
1056 (struct rte_eth_fdir_filter *)arg,
1059 case RTE_ETH_FILTER_DELETE:
1060 ret = i40e_add_del_fdir_filter(dev,
1061 (struct rte_eth_fdir_filter *)arg,
1064 case RTE_ETH_FILTER_FLUSH:
1065 ret = i40e_fdir_flush(dev);
1067 case RTE_ETH_FILTER_INFO:
1068 i40e_fdir_info_get(dev, (struct rte_eth_fdir_info *)arg);
1070 case RTE_ETH_FILTER_STATS:
1071 i40e_fdir_stats_get(dev, (struct rte_eth_fdir_stats *)arg);
1074 PMD_DRV_LOG(ERR, "unknown operation %u.", filter_op);