1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 // Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. 3 4 #include <linux/if_vlan.h> 5 #include "act.h" 6 #include "pedit.h" 7 #include "en/tc_priv.h" 8 #include "en/mod_hdr.h" 9 10 static int pedit_header_offsets[] = { 11 [FLOW_ACT_MANGLE_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth), 12 [FLOW_ACT_MANGLE_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4), 13 [FLOW_ACT_MANGLE_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6), 14 [FLOW_ACT_MANGLE_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp), 15 [FLOW_ACT_MANGLE_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp), 16 }; 17 18 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype]) 19 20 static int 21 set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset, 22 struct pedit_headers_action *hdrs, 23 struct netlink_ext_ack *extack) 24 { 25 u32 *curr_pmask, *curr_pval; 26 27 curr_pmask = (u32 *)(pedit_header(&hdrs->masks, hdr_type) + offset); 28 curr_pval = (u32 *)(pedit_header(&hdrs->vals, hdr_type) + offset); 29 30 if (*curr_pmask & mask) { /* disallow acting twice on the same location */ 31 NL_SET_ERR_MSG_MOD(extack, 32 "curr_pmask and new mask same. Acting twice on same location"); 33 goto out_err; 34 } 35 36 *curr_pmask |= mask; 37 *curr_pval |= (val & mask); 38 39 return 0; 40 41 out_err: 42 return -EOPNOTSUPP; 43 } 44 45 static int 46 parse_pedit_to_modify_hdr(struct mlx5e_priv *priv, 47 const struct flow_action_entry *act, int namespace, 48 struct mlx5e_tc_flow_parse_attr *parse_attr, 49 struct pedit_headers_action *hdrs, 50 struct netlink_ext_ack *extack) 51 { 52 u8 cmd = (act->id == FLOW_ACTION_MANGLE) ? 0 : 1; 53 u8 htype = act->mangle.htype; 54 int err = -EOPNOTSUPP; 55 u32 mask, val, offset; 56 57 if (htype == FLOW_ACT_MANGLE_UNSPEC) { 58 NL_SET_ERR_MSG_MOD(extack, "legacy pedit isn't offloaded"); 59 goto out_err; 60 } 61 62 if (!mlx5e_mod_hdr_max_actions(priv->mdev, namespace)) { 63 NL_SET_ERR_MSG_MOD(extack, "The pedit offload action is not supported"); 64 goto out_err; 65 } 66 67 mask = act->mangle.mask; 68 val = act->mangle.val; 69 offset = act->mangle.offset; 70 71 err = set_pedit_val(htype, ~mask, val, offset, &hdrs[cmd], extack); 72 if (err) 73 goto out_err; 74 75 hdrs[cmd].pedits++; 76 77 return 0; 78 out_err: 79 return err; 80 } 81 82 static int 83 parse_pedit_to_reformat(const struct flow_action_entry *act, 84 struct mlx5e_tc_flow_parse_attr *parse_attr, 85 struct netlink_ext_ack *extack) 86 { 87 u32 mask, val, offset; 88 u32 *p; 89 90 if (act->id != FLOW_ACTION_MANGLE) { 91 NL_SET_ERR_MSG_MOD(extack, "Unsupported action id"); 92 return -EOPNOTSUPP; 93 } 94 95 if (act->mangle.htype != FLOW_ACT_MANGLE_HDR_TYPE_ETH) { 96 NL_SET_ERR_MSG_MOD(extack, "Only Ethernet modification is supported"); 97 return -EOPNOTSUPP; 98 } 99 100 mask = ~act->mangle.mask; 101 val = act->mangle.val; 102 offset = act->mangle.offset; 103 p = (u32 *)&parse_attr->eth; 104 *(p + (offset >> 2)) |= (val & mask); 105 106 return 0; 107 } 108 109 int 110 mlx5e_tc_act_pedit_parse_action(struct mlx5e_priv *priv, 111 const struct flow_action_entry *act, int namespace, 112 struct mlx5e_tc_flow_parse_attr *parse_attr, 113 struct pedit_headers_action *hdrs, 114 struct mlx5e_tc_flow *flow, 115 struct netlink_ext_ack *extack) 116 { 117 if (flow && flow_flag_test(flow, L3_TO_L2_DECAP)) 118 return parse_pedit_to_reformat(act, parse_attr, extack); 119 120 return parse_pedit_to_modify_hdr(priv, act, namespace, parse_attr, hdrs, extack); 121 } 122 123 static bool 124 tc_act_can_offload_pedit(struct mlx5e_tc_act_parse_state *parse_state, 125 const struct flow_action_entry *act, 126 int act_index) 127 { 128 return true; 129 } 130 131 static int 132 tc_act_parse_pedit(struct mlx5e_tc_act_parse_state *parse_state, 133 const struct flow_action_entry *act, 134 struct mlx5e_priv *priv, 135 struct mlx5_flow_attr *attr) 136 { 137 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr; 138 struct mlx5e_tc_flow *flow = parse_state->flow; 139 enum mlx5_flow_namespace_type ns_type; 140 int err; 141 142 ns_type = mlx5e_get_flow_namespace(flow); 143 144 err = mlx5e_tc_act_pedit_parse_action(flow->priv, act, ns_type, 145 attr->parse_attr, parse_state->hdrs, 146 flow, parse_state->extack); 147 if (err) 148 return err; 149 150 if (flow_flag_test(flow, L3_TO_L2_DECAP)) 151 goto out; 152 153 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR; 154 155 if (ns_type == MLX5_FLOW_NAMESPACE_FDB) 156 esw_attr->split_count = esw_attr->out_count; 157 158 out: 159 return 0; 160 } 161 162 struct mlx5e_tc_act mlx5e_tc_act_pedit = { 163 .can_offload = tc_act_can_offload_pedit, 164 .parse_action = tc_act_parse_pedit, 165 }; 166