1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
5 #define MAX_PATTERN_NUM 3
6 #define MAX_ACTION_NUM 2
9 generate_ipv4_flow(uint16_t port_id
, uint16_t rx_q
,
10 uint32_t src_ip
, uint32_t src_mask
,
11 uint32_t dest_ip
, uint32_t dest_mask
,
12 struct rte_flow_error
*error
);
16 * create a flow rule that sends packets with matching src and dest ip
22 * The selected target queue.
24 * The src ip value to match the input packet.
26 * The mask to apply to the src ip.
28 * The dest ip value to match the input packet.
30 * The mask to apply to the dest ip.
32 * Perform verbose error reporting if not NULL.
35 * A flow if the rule could be created else return NULL.
38 generate_ipv4_flow(uint16_t port_id
, uint16_t rx_q
,
39 uint32_t src_ip
, uint32_t src_mask
,
40 uint32_t dest_ip
, uint32_t dest_mask
,
41 struct rte_flow_error
*error
)
43 struct rte_flow_attr attr
;
44 struct rte_flow_item pattern
[MAX_PATTERN_NUM
];
45 struct rte_flow_action action
[MAX_ACTION_NUM
];
46 struct rte_flow
*flow
= NULL
;
47 struct rte_flow_action_queue queue
= { .index
= rx_q
};
48 struct rte_flow_item_ipv4 ip_spec
;
49 struct rte_flow_item_ipv4 ip_mask
;
52 memset(pattern
, 0, sizeof(pattern
));
53 memset(action
, 0, sizeof(action
));
56 * set the rule attribute.
57 * in this case only ingress packets will be checked.
59 memset(&attr
, 0, sizeof(struct rte_flow_attr
));
63 * create the action sequence.
64 * one action only, move packet to queue
66 action
[0].type
= RTE_FLOW_ACTION_TYPE_QUEUE
;
67 action
[0].conf
= &queue
;
68 action
[1].type
= RTE_FLOW_ACTION_TYPE_END
;
71 * set the first level of the pattern (ETH).
72 * since in this example we just want to get the
73 * ipv4 we set this level to allow all.
75 pattern
[0].type
= RTE_FLOW_ITEM_TYPE_ETH
;
78 * setting the second level of the pattern (IP).
79 * in this example this is the level we care about
80 * so we set it according to the parameters.
82 memset(&ip_spec
, 0, sizeof(struct rte_flow_item_ipv4
));
83 memset(&ip_mask
, 0, sizeof(struct rte_flow_item_ipv4
));
84 ip_spec
.hdr
.dst_addr
= htonl(dest_ip
);
85 ip_mask
.hdr
.dst_addr
= dest_mask
;
86 ip_spec
.hdr
.src_addr
= htonl(src_ip
);
87 ip_mask
.hdr
.src_addr
= src_mask
;
88 pattern
[1].type
= RTE_FLOW_ITEM_TYPE_IPV4
;
89 pattern
[1].spec
= &ip_spec
;
90 pattern
[1].mask
= &ip_mask
;
92 /* the final level must be always type end */
93 pattern
[2].type
= RTE_FLOW_ITEM_TYPE_END
;
95 res
= rte_flow_validate(port_id
, &attr
, pattern
, action
, error
);
97 flow
= rte_flow_create(port_id
, &attr
, pattern
, action
, error
);