1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
5 #define MAX_PATTERN_NUM 4
8 generate_ipv4_flow(uint16_t port_id
, uint16_t rx_q
,
9 uint32_t src_ip
, uint32_t src_mask
,
10 uint32_t dest_ip
, uint32_t dest_mask
,
11 struct rte_flow_error
*error
);
15 * create a flow rule that sends packets with matching src and dest ip
21 * The selected target queue.
23 * The src ip value to match the input packet.
25 * The mask to apply to the src ip.
27 * The dest ip value to match the input packet.
29 * The mask to apply to the dest ip.
31 * Perform verbose error reporting if not NULL.
34 * A flow if the rule could be created else return NULL.
37 generate_ipv4_flow(uint16_t port_id
, uint16_t rx_q
,
38 uint32_t src_ip
, uint32_t src_mask
,
39 uint32_t dest_ip
, uint32_t dest_mask
,
40 struct rte_flow_error
*error
)
42 struct rte_flow_attr attr
;
43 struct rte_flow_item pattern
[MAX_PATTERN_NUM
];
44 struct rte_flow_action action
[MAX_PATTERN_NUM
];
45 struct rte_flow
*flow
= NULL
;
46 struct rte_flow_action_queue queue
= { .index
= rx_q
};
47 struct rte_flow_item_eth eth_spec
;
48 struct rte_flow_item_eth eth_mask
;
49 struct rte_flow_item_vlan vlan_spec
;
50 struct rte_flow_item_vlan vlan_mask
;
51 struct rte_flow_item_ipv4 ip_spec
;
52 struct rte_flow_item_ipv4 ip_mask
;
55 memset(pattern
, 0, sizeof(pattern
));
56 memset(action
, 0, sizeof(action
));
59 * set the rule attribute.
60 * in this case only ingress packets will be checked.
62 memset(&attr
, 0, sizeof(struct rte_flow_attr
));
66 * create the action sequence.
67 * one action only, move packet to queue
70 action
[0].type
= RTE_FLOW_ACTION_TYPE_QUEUE
;
71 action
[0].conf
= &queue
;
72 action
[1].type
= RTE_FLOW_ACTION_TYPE_END
;
75 * set the first level of the pattern (eth).
76 * since in this example we just want to get the
77 * ipv4 we set this level to allow all.
79 memset(ð_spec
, 0, sizeof(struct rte_flow_item_eth
));
80 memset(ð_mask
, 0, sizeof(struct rte_flow_item_eth
));
83 pattern
[0].type
= RTE_FLOW_ITEM_TYPE_ETH
;
84 pattern
[0].spec
= ð_spec
;
85 pattern
[0].mask
= ð_mask
;
88 * setting the second level of the pattern (vlan).
89 * since in this example we just want to get the
90 * ipv4 we also set this level to allow all.
92 memset(&vlan_spec
, 0, sizeof(struct rte_flow_item_vlan
));
93 memset(&vlan_mask
, 0, sizeof(struct rte_flow_item_vlan
));
94 pattern
[1].type
= RTE_FLOW_ITEM_TYPE_VLAN
;
95 pattern
[1].spec
= &vlan_spec
;
96 pattern
[1].mask
= &vlan_mask
;
99 * setting the third level of the pattern (ip).
100 * in this example this is the level we care about
101 * so we set it according to the parameters.
103 memset(&ip_spec
, 0, sizeof(struct rte_flow_item_ipv4
));
104 memset(&ip_mask
, 0, sizeof(struct rte_flow_item_ipv4
));
105 ip_spec
.hdr
.dst_addr
= htonl(dest_ip
);
106 ip_mask
.hdr
.dst_addr
= dest_mask
;
107 ip_spec
.hdr
.src_addr
= htonl(src_ip
);
108 ip_mask
.hdr
.src_addr
= src_mask
;
109 pattern
[2].type
= RTE_FLOW_ITEM_TYPE_IPV4
;
110 pattern
[2].spec
= &ip_spec
;
111 pattern
[2].mask
= &ip_mask
;
113 /* the final level must be always type end */
114 pattern
[3].type
= RTE_FLOW_ITEM_TYPE_END
;
116 res
= rte_flow_validate(port_id
, &attr
, pattern
, action
, error
);
118 flow
= rte_flow_create(port_id
, &attr
, pattern
, action
, error
);