2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
4 * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_mirred.h>
39 #include "cxgb4_tc_u32_parse.h"
40 #include "cxgb4_tc_u32.h"
42 /* Fill ch_filter_specification with parsed match value/mask pair. */
43 static int fill_match_fields(struct adapter *adap,
44 struct ch_filter_specification *fs,
45 struct tc_cls_u32_offload *cls,
46 const struct cxgb4_match_field *entry,
54 for (i = 0; i < cls->knode.sel->nkeys; i++) {
55 off = cls->knode.sel->keys[i].off;
56 val = cls->knode.sel->keys[i].val;
57 mask = cls->knode.sel->keys[i].mask;
60 /* For next headers, parse only keys with offmask */
61 if (!cls->knode.sel->keys[i].offmask)
64 /* For the remaining, parse only keys without offmask */
65 if (cls->knode.sel->keys[i].offmask)
71 for (j = 0; entry[j].val; j++) {
72 if (off == entry[j].off) {
74 err = entry[j].val(fs, val, mask);
88 /* Fill ch_filter_specification with parsed action. */
89 static int fill_action_fields(struct adapter *adap,
90 struct ch_filter_specification *fs,
91 struct tc_cls_u32_offload *cls)
93 unsigned int num_actions = 0;
94 const struct tc_action *a;
95 struct tcf_exts *exts;
98 exts = cls->knode.exts;
99 if (!tcf_exts_has_actions(exts))
102 tcf_exts_for_each_action(i, a, exts) {
103 /* Don't allow more than one action per rule. */
107 /* Drop in hardware. */
108 if (is_tcf_gact_shot(a)) {
109 fs->action = FILTER_DROP;
114 /* Re-direct to specified port in hardware. */
115 if (is_tcf_mirred_egress_redirect(a)) {
116 struct net_device *n_dev, *target_dev;
120 target_dev = tcf_mirred_dev(a);
121 for_each_port(adap, i) {
122 n_dev = adap->port[i];
123 if (target_dev == n_dev) {
124 fs->action = FILTER_SWITCH;
131 /* Interface doesn't belong to any port of
132 * the underlying hardware.
141 /* Un-supported action. */
148 int cxgb4_config_knode(struct net_device *dev, struct tc_cls_u32_offload *cls)
150 const struct cxgb4_match_field *start, *link_start = NULL;
151 struct adapter *adapter = netdev2adap(dev);
152 __be16 protocol = cls->common.protocol;
153 struct ch_filter_specification fs;
154 struct cxgb4_tc_u32_table *t;
155 struct cxgb4_link *link;
156 unsigned int filter_id;
157 u32 uhtid, link_uhtid;
158 bool is_ipv6 = false;
161 if (!can_tc_u32_offload(dev))
164 if (protocol != htons(ETH_P_IP) && protocol != htons(ETH_P_IPV6))
167 /* Fetch the location to insert the filter. */
168 filter_id = cls->knode.handle & 0xFFFFF;
170 if (filter_id > adapter->tids.nftids) {
171 dev_err(adapter->pdev_dev,
172 "Location %d out of range for insertion. Max: %d\n",
173 filter_id, adapter->tids.nftids);
178 uhtid = TC_U32_USERHTID(cls->knode.handle);
179 link_uhtid = TC_U32_USERHTID(cls->knode.link_handle);
181 /* Ensure that uhtid is either root u32 (i.e. 0x800)
182 * or a a valid linked bucket.
184 if (uhtid != 0x800 && uhtid >= t->size)
187 /* Ensure link handle uhtid is sane, if specified. */
188 if (link_uhtid >= t->size)
191 memset(&fs, 0, sizeof(fs));
193 if (protocol == htons(ETH_P_IPV6)) {
194 start = cxgb4_ipv6_fields;
197 start = cxgb4_ipv4_fields;
201 if (uhtid != 0x800) {
202 /* Link must exist from root node before insertion. */
203 if (!t->table[uhtid - 1].link_handle)
206 /* Link must have a valid supported next header. */
207 link_start = t->table[uhtid - 1].match_field;
212 /* Parse links and record them for subsequent jumps to valid
216 const struct cxgb4_next_header *next;
222 if (t->table[link_uhtid - 1].link_handle) {
223 dev_err(adapter->pdev_dev,
224 "Link handle exists for: 0x%x\n",
229 next = is_ipv6 ? cxgb4_ipv6_jumps : cxgb4_ipv4_jumps;
231 /* Try to find matches that allow jumps to next header. */
232 for (i = 0; next[i].jump; i++) {
233 if (next[i].offoff != cls->knode.sel->offoff ||
234 next[i].shift != cls->knode.sel->offshift ||
235 next[i].mask != cls->knode.sel->offmask ||
236 next[i].offset != cls->knode.sel->off)
239 /* Found a possible candidate. Find a key that
240 * matches the corresponding offset, value, and
241 * mask to jump to next header.
243 for (j = 0; j < cls->knode.sel->nkeys; j++) {
244 off = cls->knode.sel->keys[j].off;
245 val = cls->knode.sel->keys[j].val;
246 mask = cls->knode.sel->keys[j].mask;
248 if (next[i].match_off == off &&
249 next[i].match_val == val &&
250 next[i].match_mask == mask) {
257 continue; /* Try next candidate. */
259 /* Candidate to jump to next header found.
260 * Translate all keys to internal specification
261 * and store them in jump table. This spec is copied
262 * later to set the actual filters.
264 ret = fill_match_fields(adapter, &fs, cls,
269 link = &t->table[link_uhtid - 1];
270 link->match_field = next[i].jump;
271 link->link_handle = cls->knode.handle;
272 memcpy(&link->fs, &fs, sizeof(fs));
276 /* No candidate found to jump to next header. */
283 /* Fill ch_filter_specification match fields to be shipped to hardware.
284 * Copy the linked spec (if any) first. And then update the spec as
287 if (uhtid != 0x800 && t->table[uhtid - 1].link_handle) {
288 /* Copy linked ch_filter_specification */
289 memcpy(&fs, &t->table[uhtid - 1].fs, sizeof(fs));
290 ret = fill_match_fields(adapter, &fs, cls,
296 ret = fill_match_fields(adapter, &fs, cls, start, false);
300 /* Fill ch_filter_specification action fields to be shipped to
303 ret = fill_action_fields(adapter, &fs, cls);
307 /* The filter spec has been completely built from the info
308 * provided from u32. We now set some default fields in the
312 /* Match only packets coming from the ingress port where this
313 * filter will be created.
315 fs.val.iport = netdev2pinfo(dev)->port_id;
318 /* Enable filter hit counts. */
321 /* Set type of filter - IPv6 or IPv4 */
322 fs.type = is_ipv6 ? 1 : 0;
325 ret = cxgb4_set_filter(dev, filter_id, &fs);
329 /* If this is a linked bucket, then set the corresponding
330 * entry in the bitmap to mark it as belonging to this linked
333 if (uhtid != 0x800 && t->table[uhtid - 1].link_handle)
334 set_bit(filter_id, t->table[uhtid - 1].tid_map);
340 int cxgb4_delete_knode(struct net_device *dev, struct tc_cls_u32_offload *cls)
342 struct adapter *adapter = netdev2adap(dev);
343 unsigned int filter_id, max_tids, i, j;
344 struct cxgb4_link *link = NULL;
345 struct cxgb4_tc_u32_table *t;
349 if (!can_tc_u32_offload(dev))
352 /* Fetch the location to delete the filter. */
353 filter_id = cls->knode.handle & 0xFFFFF;
355 if (filter_id > adapter->tids.nftids) {
356 dev_err(adapter->pdev_dev,
357 "Location %d out of range for deletion. Max: %d\n",
358 filter_id, adapter->tids.nftids);
363 handle = cls->knode.handle;
364 uhtid = TC_U32_USERHTID(cls->knode.handle);
366 /* Ensure that uhtid is either root u32 (i.e. 0x800)
367 * or a a valid linked bucket.
369 if (uhtid != 0x800 && uhtid >= t->size)
372 /* Delete the specified filter */
373 if (uhtid != 0x800) {
374 link = &t->table[uhtid - 1];
375 if (!link->link_handle)
378 if (!test_bit(filter_id, link->tid_map))
382 ret = cxgb4_del_filter(dev, filter_id, NULL);
387 clear_bit(filter_id, link->tid_map);
389 /* If a link is being deleted, then delete all filters
390 * associated with the link.
392 max_tids = adapter->tids.nftids;
393 for (i = 0; i < t->size; i++) {
396 if (link->link_handle == handle) {
397 for (j = 0; j < max_tids; j++) {
398 if (!test_bit(j, link->tid_map))
401 ret = __cxgb4_del_filter(dev, j, NULL, NULL);
405 clear_bit(j, link->tid_map);
408 /* Clear the link state */
409 link->match_field = NULL;
410 link->link_handle = 0;
411 memset(&link->fs, 0, sizeof(link->fs));
420 void cxgb4_cleanup_tc_u32(struct adapter *adap)
422 struct cxgb4_tc_u32_table *t;
428 /* Free up all allocated memory. */
430 for (i = 0; i < t->size; i++) {
431 struct cxgb4_link *link = &t->table[i];
433 kvfree(link->tid_map);
435 kvfree(adap->tc_u32);
438 struct cxgb4_tc_u32_table *cxgb4_init_tc_u32(struct adapter *adap)
440 unsigned int max_tids = adap->tids.nftids;
441 struct cxgb4_tc_u32_table *t;
447 t = kvzalloc(sizeof(*t) +
448 (max_tids * sizeof(struct cxgb4_link)), GFP_KERNEL);
454 for (i = 0; i < t->size; i++) {
455 struct cxgb4_link *link = &t->table[i];
456 unsigned int bmap_size;
458 bmap_size = BITS_TO_LONGS(max_tids);
459 link->tid_map = kvcalloc(bmap_size, sizeof(unsigned long),
463 bitmap_zero(link->tid_map, max_tids);
469 for (i = 0; i < t->size; i++) {
470 struct cxgb4_link *link = &t->table[i];
473 kvfree(link->tid_map);