4 * Copyright (c) 2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/queue.h>
41 #include <rte_ether.h>
42 #include <rte_ethdev.h>
44 #include <rte_memzone.h>
45 #include <rte_malloc.h>
46 #include <rte_eth_ctrl.h>
47 #include <rte_tailq.h>
48 #include <rte_flow_driver.h>
50 #include "i40e_logs.h"
51 #include "base/i40e_type.h"
52 #include "base/i40e_prototype.h"
53 #include "i40e_ethdev.h"
55 static int i40e_flow_validate(struct rte_eth_dev *dev,
56 const struct rte_flow_attr *attr,
57 const struct rte_flow_item pattern[],
58 const struct rte_flow_action actions[],
59 struct rte_flow_error *error);
61 i40e_flow_parse_ethertype_pattern(struct rte_eth_dev *dev,
62 const struct rte_flow_item *pattern,
63 struct rte_flow_error *error,
64 struct rte_eth_ethertype_filter *filter);
65 static int i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
66 const struct rte_flow_action *actions,
67 struct rte_flow_error *error,
68 struct rte_eth_ethertype_filter *filter);
69 static int i40e_flow_parse_attr(const struct rte_flow_attr *attr,
70 struct rte_flow_error *error);
71 static int i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
72 const struct rte_flow_attr *attr,
73 const struct rte_flow_item pattern[],
74 const struct rte_flow_action actions[],
75 struct rte_flow_error *error,
76 union i40e_filter_t *filter);
78 const struct rte_flow_ops i40e_flow_ops = {
79 .validate = i40e_flow_validate,
82 union i40e_filter_t cons_filter;
84 /* Pattern matched ethertype filter */
85 static enum rte_flow_item_type pattern_ethertype[] = {
86 RTE_FLOW_ITEM_TYPE_ETH,
87 RTE_FLOW_ITEM_TYPE_END,
90 static struct i40e_valid_pattern i40e_supported_patterns[] = {
92 { pattern_ethertype, i40e_flow_parse_ethertype_filter },
95 #define NEXT_ITEM_OF_ACTION(act, actions, index) \
97 act = actions + index; \
98 while (act->type == RTE_FLOW_ACTION_TYPE_VOID) { \
100 act = actions + index; \
104 /* Find the first VOID or non-VOID item pointer */
105 static const struct rte_flow_item *
106 i40e_find_first_item(const struct rte_flow_item *item, bool is_void)
110 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
112 is_find = item->type == RTE_FLOW_ITEM_TYPE_VOID;
114 is_find = item->type != RTE_FLOW_ITEM_TYPE_VOID;
122 /* Skip all VOID items of the pattern */
124 i40e_pattern_skip_void_item(struct rte_flow_item *items,
125 const struct rte_flow_item *pattern)
127 uint32_t cpy_count = 0;
128 const struct rte_flow_item *pb = pattern, *pe = pattern;
131 /* Find a non-void item first */
132 pb = i40e_find_first_item(pb, false);
133 if (pb->type == RTE_FLOW_ITEM_TYPE_END) {
138 /* Find a void item */
139 pe = i40e_find_first_item(pb + 1, true);
142 rte_memcpy(items, pb, sizeof(struct rte_flow_item) * cpy_count);
146 if (pe->type == RTE_FLOW_ITEM_TYPE_END) {
153 /* Copy the END item. */
154 rte_memcpy(items, pe, sizeof(struct rte_flow_item));
157 /* Check if the pattern matches a supported item type array */
159 i40e_match_pattern(enum rte_flow_item_type *item_array,
160 struct rte_flow_item *pattern)
162 struct rte_flow_item *item = pattern;
164 while ((*item_array == item->type) &&
165 (*item_array != RTE_FLOW_ITEM_TYPE_END)) {
170 return (*item_array == RTE_FLOW_ITEM_TYPE_END &&
171 item->type == RTE_FLOW_ITEM_TYPE_END);
174 /* Find if there's parse filter function matched */
175 static parse_filter_t
176 i40e_find_parse_filter_func(struct rte_flow_item *pattern)
178 parse_filter_t parse_filter = NULL;
181 for (; i < RTE_DIM(i40e_supported_patterns); i++) {
182 if (i40e_match_pattern(i40e_supported_patterns[i].items,
184 parse_filter = i40e_supported_patterns[i].parse_filter;
192 /* Parse attributes */
194 i40e_flow_parse_attr(const struct rte_flow_attr *attr,
195 struct rte_flow_error *error)
197 /* Must be input direction */
198 if (!attr->ingress) {
199 rte_flow_error_set(error, EINVAL,
200 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
201 attr, "Only support ingress.");
207 rte_flow_error_set(error, EINVAL,
208 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
209 attr, "Not support egress.");
214 if (attr->priority) {
215 rte_flow_error_set(error, EINVAL,
216 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
217 attr, "Not support priority.");
223 rte_flow_error_set(error, EINVAL,
224 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
225 attr, "Not support group.");
233 i40e_get_outer_vlan(struct rte_eth_dev *dev)
235 struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
236 int qinq = dev->data->dev_conf.rxmode.hw_vlan_extend;
246 i40e_aq_debug_read_register(hw, I40E_GL_SWT_L2TAGCTRL(reg_id),
249 tpid = (reg_r >> I40E_GL_SWT_L2TAGCTRL_ETHERTYPE_SHIFT) & 0xFFFF;
254 /* 1. Last in item should be NULL as range is not supported.
255 * 2. Supported filter types: MAC_ETHTYPE and ETHTYPE.
256 * 3. SRC mac_addr mask should be 00:00:00:00:00:00.
257 * 4. DST mac_addr mask should be 00:00:00:00:00:00 or
259 * 5. Ether_type mask should be 0xFFFF.
262 i40e_flow_parse_ethertype_pattern(struct rte_eth_dev *dev,
263 const struct rte_flow_item *pattern,
264 struct rte_flow_error *error,
265 struct rte_eth_ethertype_filter *filter)
267 const struct rte_flow_item *item = pattern;
268 const struct rte_flow_item_eth *eth_spec;
269 const struct rte_flow_item_eth *eth_mask;
270 enum rte_flow_item_type item_type;
273 outer_tpid = i40e_get_outer_vlan(dev);
275 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
277 rte_flow_error_set(error, EINVAL,
278 RTE_FLOW_ERROR_TYPE_ITEM,
280 "Not support range");
283 item_type = item->type;
285 case RTE_FLOW_ITEM_TYPE_ETH:
286 eth_spec = (const struct rte_flow_item_eth *)item->spec;
287 eth_mask = (const struct rte_flow_item_eth *)item->mask;
288 /* Get the MAC info. */
289 if (!eth_spec || !eth_mask) {
290 rte_flow_error_set(error, EINVAL,
291 RTE_FLOW_ERROR_TYPE_ITEM,
293 "NULL ETH spec/mask");
297 /* Mask bits of source MAC address must be full of 0.
298 * Mask bits of destination MAC address must be full
301 if (!is_zero_ether_addr(ð_mask->src) ||
302 (!is_zero_ether_addr(ð_mask->dst) &&
303 !is_broadcast_ether_addr(ð_mask->dst))) {
304 rte_flow_error_set(error, EINVAL,
305 RTE_FLOW_ERROR_TYPE_ITEM,
307 "Invalid MAC_addr mask");
311 if ((eth_mask->type & UINT16_MAX) != UINT16_MAX) {
312 rte_flow_error_set(error, EINVAL,
313 RTE_FLOW_ERROR_TYPE_ITEM,
315 "Invalid ethertype mask");
319 /* If mask bits of destination MAC address
320 * are full of 1, set RTE_ETHTYPE_FLAGS_MAC.
322 if (is_broadcast_ether_addr(ð_mask->dst)) {
323 filter->mac_addr = eth_spec->dst;
324 filter->flags |= RTE_ETHTYPE_FLAGS_MAC;
326 filter->flags &= ~RTE_ETHTYPE_FLAGS_MAC;
328 filter->ether_type = rte_be_to_cpu_16(eth_spec->type);
330 if (filter->ether_type == ETHER_TYPE_IPv4 ||
331 filter->ether_type == ETHER_TYPE_IPv6 ||
332 filter->ether_type == outer_tpid) {
333 rte_flow_error_set(error, EINVAL,
334 RTE_FLOW_ERROR_TYPE_ITEM,
336 "Unsupported ether_type in"
337 " control packet filter.");
349 /* Ethertype action only supports QUEUE or DROP. */
351 i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
352 const struct rte_flow_action *actions,
353 struct rte_flow_error *error,
354 struct rte_eth_ethertype_filter *filter)
356 struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
357 const struct rte_flow_action *act;
358 const struct rte_flow_action_queue *act_q;
361 /* Check if the first non-void action is QUEUE or DROP. */
362 NEXT_ITEM_OF_ACTION(act, actions, index);
363 if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
364 act->type != RTE_FLOW_ACTION_TYPE_DROP) {
365 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
366 act, "Not supported action.");
370 if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
371 act_q = (const struct rte_flow_action_queue *)act->conf;
372 filter->queue = act_q->index;
373 if (filter->queue >= pf->dev_data->nb_rx_queues) {
374 rte_flow_error_set(error, EINVAL,
375 RTE_FLOW_ERROR_TYPE_ACTION,
376 act, "Invalid queue ID for"
377 " ethertype_filter.");
381 filter->flags |= RTE_ETHTYPE_FLAGS_DROP;
384 /* Check if the next non-void item is END */
386 NEXT_ITEM_OF_ACTION(act, actions, index);
387 if (act->type != RTE_FLOW_ACTION_TYPE_END) {
388 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
389 act, "Not supported action.");
397 i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
398 const struct rte_flow_attr *attr,
399 const struct rte_flow_item pattern[],
400 const struct rte_flow_action actions[],
401 struct rte_flow_error *error,
402 union i40e_filter_t *filter)
404 struct rte_eth_ethertype_filter *ethertype_filter =
405 &filter->ethertype_filter;
408 ret = i40e_flow_parse_ethertype_pattern(dev, pattern, error,
413 ret = i40e_flow_parse_ethertype_action(dev, actions, error,
418 ret = i40e_flow_parse_attr(attr, error);
426 i40e_flow_validate(struct rte_eth_dev *dev,
427 const struct rte_flow_attr *attr,
428 const struct rte_flow_item pattern[],
429 const struct rte_flow_action actions[],
430 struct rte_flow_error *error)
432 struct rte_flow_item *items; /* internal pattern w/o VOID items */
433 parse_filter_t parse_filter;
434 uint32_t item_num = 0; /* non-void item number of pattern*/
439 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
440 NULL, "NULL pattern.");
445 rte_flow_error_set(error, EINVAL,
446 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
447 NULL, "NULL action.");
452 rte_flow_error_set(error, EINVAL,
453 RTE_FLOW_ERROR_TYPE_ATTR,
454 NULL, "NULL attribute.");
458 memset(&cons_filter, 0, sizeof(cons_filter));
460 /* Get the non-void item number of pattern */
461 while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
462 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
468 items = rte_zmalloc("i40e_pattern",
469 item_num * sizeof(struct rte_flow_item), 0);
471 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
472 NULL, "No memory for PMD internal items.");
476 i40e_pattern_skip_void_item(items, pattern);
478 /* Find if there's matched parse filter function */
479 parse_filter = i40e_find_parse_filter_func(items);
481 rte_flow_error_set(error, EINVAL,
482 RTE_FLOW_ERROR_TYPE_ITEM,
483 pattern, "Unsupported pattern");
487 ret = parse_filter(dev, attr, items, actions, error, &cons_filter);