2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/bitfield.h>
35 #include <net/pkt_cls.h>
36 #include <net/switchdev.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_mirred.h>
39 #include <net/tc_act/tc_vlan.h>
43 #include "../nfp_net_repr.h"
45 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan
*pop_vlan
)
47 size_t act_size
= sizeof(struct nfp_fl_pop_vlan
);
51 FIELD_PREP(NFP_FL_ACT_LEN_LW
, act_size
>> NFP_FL_LW_SIZ
) |
52 FIELD_PREP(NFP_FL_ACT_JMP_ID
, NFP_FL_ACTION_OPCODE_POP_VLAN
);
54 pop_vlan
->a_op
= cpu_to_be16(tmp_pop_vlan_op
);
55 pop_vlan
->reserved
= 0;
59 nfp_fl_push_vlan(struct nfp_fl_push_vlan
*push_vlan
,
60 const struct tc_action
*action
)
62 size_t act_size
= sizeof(struct nfp_fl_push_vlan
);
63 struct tcf_vlan
*vlan
= to_vlan(action
);
64 u16 tmp_push_vlan_tci
;
68 FIELD_PREP(NFP_FL_ACT_LEN_LW
, act_size
>> NFP_FL_LW_SIZ
) |
69 FIELD_PREP(NFP_FL_ACT_JMP_ID
, NFP_FL_ACTION_OPCODE_PUSH_VLAN
);
71 push_vlan
->a_op
= cpu_to_be16(tmp_push_vlan_op
);
72 /* Set action push vlan parameters. */
73 push_vlan
->reserved
= 0;
74 push_vlan
->vlan_tpid
= tcf_vlan_push_proto(action
);
77 FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO
, vlan
->tcfv_push_prio
) |
78 FIELD_PREP(NFP_FL_PUSH_VLAN_VID
, vlan
->tcfv_push_vid
) |
80 push_vlan
->vlan_tci
= cpu_to_be16(tmp_push_vlan_tci
);
84 nfp_fl_output(struct nfp_fl_output
*output
, const struct tc_action
*action
,
85 struct nfp_fl_payload
*nfp_flow
, bool last
,
86 struct net_device
*in_dev
)
88 size_t act_size
= sizeof(struct nfp_fl_output
);
89 struct net_device
*out_dev
;
93 /* Set action opcode to output action. */
95 FIELD_PREP(NFP_FL_ACT_LEN_LW
, act_size
>> NFP_FL_LW_SIZ
) |
96 FIELD_PREP(NFP_FL_ACT_JMP_ID
, NFP_FL_ACTION_OPCODE_OUTPUT
);
98 output
->a_op
= cpu_to_be16(tmp_output_op
);
100 /* Set action output parameters. */
101 output
->flags
= cpu_to_be16(last
? NFP_FL_OUT_FLAGS_LAST
: 0);
103 ifindex
= tcf_mirred_ifindex(action
);
104 out_dev
= __dev_get_by_index(dev_net(in_dev
), ifindex
);
108 /* Only offload egress ports are on the same device as the ingress
111 if (!switchdev_port_same_parent_id(in_dev
, out_dev
))
113 if (!nfp_netdev_is_nfp_repr(out_dev
))
116 output
->port
= cpu_to_be32(nfp_repr_get_port_id(out_dev
));
120 nfp_flow
->meta
.shortcut
= output
->port
;
126 nfp_flower_loop_action(const struct tc_action
*a
,
127 struct nfp_fl_payload
*nfp_fl
, int *a_len
,
128 struct net_device
*netdev
)
130 struct nfp_fl_push_vlan
*psh_v
;
131 struct nfp_fl_pop_vlan
*pop_v
;
132 struct nfp_fl_output
*output
;
135 if (is_tcf_gact_shot(a
)) {
136 nfp_fl
->meta
.shortcut
= cpu_to_be32(NFP_FL_SC_ACT_DROP
);
137 } else if (is_tcf_mirred_egress_redirect(a
)) {
138 if (*a_len
+ sizeof(struct nfp_fl_output
) > NFP_FL_MAX_A_SIZ
)
141 output
= (struct nfp_fl_output
*)&nfp_fl
->action_data
[*a_len
];
142 err
= nfp_fl_output(output
, a
, nfp_fl
, true, netdev
);
146 *a_len
+= sizeof(struct nfp_fl_output
);
147 } else if (is_tcf_mirred_egress_mirror(a
)) {
148 if (*a_len
+ sizeof(struct nfp_fl_output
) > NFP_FL_MAX_A_SIZ
)
151 output
= (struct nfp_fl_output
*)&nfp_fl
->action_data
[*a_len
];
152 err
= nfp_fl_output(output
, a
, nfp_fl
, false, netdev
);
156 *a_len
+= sizeof(struct nfp_fl_output
);
157 } else if (is_tcf_vlan(a
) && tcf_vlan_action(a
) == TCA_VLAN_ACT_POP
) {
158 if (*a_len
+ sizeof(struct nfp_fl_pop_vlan
) > NFP_FL_MAX_A_SIZ
)
161 pop_v
= (struct nfp_fl_pop_vlan
*)&nfp_fl
->action_data
[*a_len
];
162 nfp_fl
->meta
.shortcut
= cpu_to_be32(NFP_FL_SC_ACT_POPV
);
164 nfp_fl_pop_vlan(pop_v
);
165 *a_len
+= sizeof(struct nfp_fl_pop_vlan
);
166 } else if (is_tcf_vlan(a
) && tcf_vlan_action(a
) == TCA_VLAN_ACT_PUSH
) {
167 if (*a_len
+ sizeof(struct nfp_fl_push_vlan
) > NFP_FL_MAX_A_SIZ
)
170 psh_v
= (struct nfp_fl_push_vlan
*)&nfp_fl
->action_data
[*a_len
];
171 nfp_fl
->meta
.shortcut
= cpu_to_be32(NFP_FL_SC_ACT_NULL
);
173 nfp_fl_push_vlan(psh_v
, a
);
174 *a_len
+= sizeof(struct nfp_fl_push_vlan
);
176 /* Currently we do not handle any other actions. */
183 int nfp_flower_compile_action(struct tc_cls_flower_offload
*flow
,
184 struct net_device
*netdev
,
185 struct nfp_fl_payload
*nfp_flow
)
187 int act_len
, act_cnt
, err
;
188 const struct tc_action
*a
;
191 memset(nfp_flow
->action_data
, 0, NFP_FL_MAX_A_SIZ
);
192 nfp_flow
->meta
.act_len
= 0;
196 tcf_exts_to_list(flow
->exts
, &actions
);
197 list_for_each_entry(a
, &actions
, list
) {
198 err
= nfp_flower_loop_action(a
, nfp_flow
, &act_len
, netdev
);
204 /* We optimise when the action list is small, this can unfortunately
205 * not happen once we have more than one action in the action list.
208 nfp_flow
->meta
.shortcut
= cpu_to_be32(NFP_FL_SC_ACT_NULL
);
210 nfp_flow
->meta
.act_len
= act_len
;