Skip to content

Commit 1a6281a

Browse files
Ben Hutchingsdavem330
authored andcommitted
sfc: Add support for retrieving and removing filters by ID
These new functions will support an implementation of the ethtool RX NFC rules API. Signed-off-by: Ben Hutchings <bhutchings@solarflare.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3532650 commit 1a6281a

3 files changed

Lines changed: 266 additions & 0 deletions

File tree

‎drivers/net/ethernet/sfc/efx.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,20 @@ extern s32 efx_filter_insert_filter(struct efx_nic *efx,
6666
bool replace);
6767
extern int efx_filter_remove_filter(struct efx_nic *efx,
6868
struct efx_filter_spec *spec);
69+
extern int efx_filter_remove_id_safe(struct efx_nic *efx,
70+
enum efx_filter_priority priority,
71+
u32 filter_id);
72+
extern int efx_filter_get_filter_safe(struct efx_nic *efx,
73+
enum efx_filter_priority priority,
74+
u32 filter_id, struct efx_filter_spec *);
6975
extern void efx_filter_clear_rx(struct efx_nic *efx,
7076
enum efx_filter_priority priority);
77+
extern u32 efx_filter_count_rx_used(struct efx_nic *efx,
78+
enum efx_filter_priority priority);
79+
extern u32 efx_filter_get_rx_id_limit(struct efx_nic *efx);
80+
extern s32 efx_filter_get_rx_ids(struct efx_nic *efx,
81+
enum efx_filter_priority priority,
82+
u32 *buf, u32 size);
7183
#ifdef CONFIG_RFS_ACCEL
7284
extern int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
7385
u16 rxq_index, u32 flow_id);

‎drivers/net/ethernet/sfc/filter.c‎

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ static inline void __efx_filter_set_ipv4(struct efx_filter_spec *spec,
155155
spec->data[2] = ntohl(host2);
156156
}
157157

158+
static inline void __efx_filter_get_ipv4(const struct efx_filter_spec *spec,
159+
__be32 *host1, __be16 *port1,
160+
__be32 *host2, __be16 *port2)
161+
{
162+
*host1 = htonl(spec->data[0] >> 16 | spec->data[1] << 16);
163+
*port1 = htons(spec->data[0]);
164+
*host2 = htonl(spec->data[2]);
165+
*port2 = htons(spec->data[1] >> 16);
166+
}
167+
158168
/**
159169
* efx_filter_set_ipv4_local - specify IPv4 host, transport protocol and port
160170
* @spec: Specification to initialise
@@ -205,6 +215,26 @@ int efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
205215
return 0;
206216
}
207217

218+
int efx_filter_get_ipv4_local(const struct efx_filter_spec *spec,
219+
u8 *proto, __be32 *host, __be16 *port)
220+
{
221+
__be32 host1;
222+
__be16 port1;
223+
224+
switch (spec->type) {
225+
case EFX_FILTER_TCP_WILD:
226+
*proto = IPPROTO_TCP;
227+
__efx_filter_get_ipv4(spec, &host1, &port1, host, port);
228+
return 0;
229+
case EFX_FILTER_UDP_WILD:
230+
*proto = IPPROTO_UDP;
231+
__efx_filter_get_ipv4(spec, &host1, port, host, &port1);
232+
return 0;
233+
default:
234+
return -EINVAL;
235+
}
236+
}
237+
208238
/**
209239
* efx_filter_set_ipv4_full - specify IPv4 hosts, transport protocol and ports
210240
* @spec: Specification to initialise
@@ -242,6 +272,25 @@ int efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
242272
return 0;
243273
}
244274

275+
int efx_filter_get_ipv4_full(const struct efx_filter_spec *spec,
276+
u8 *proto, __be32 *host, __be16 *port,
277+
__be32 *rhost, __be16 *rport)
278+
{
279+
switch (spec->type) {
280+
case EFX_FILTER_TCP_FULL:
281+
*proto = IPPROTO_TCP;
282+
break;
283+
case EFX_FILTER_UDP_FULL:
284+
*proto = IPPROTO_UDP;
285+
break;
286+
default:
287+
return -EINVAL;
288+
}
289+
290+
__efx_filter_get_ipv4(spec, rhost, rport, host, port);
291+
return 0;
292+
}
293+
245294
/**
246295
* efx_filter_set_eth_local - specify local Ethernet address and optional VID
247296
* @spec: Specification to initialise
@@ -270,6 +319,29 @@ int efx_filter_set_eth_local(struct efx_filter_spec *spec,
270319
return 0;
271320
}
272321

322+
int efx_filter_get_eth_local(const struct efx_filter_spec *spec,
323+
u16 *vid, u8 *addr)
324+
{
325+
switch (spec->type) {
326+
case EFX_FILTER_MAC_WILD:
327+
*vid = EFX_FILTER_VID_UNSPEC;
328+
break;
329+
case EFX_FILTER_MAC_FULL:
330+
*vid = spec->data[0];
331+
break;
332+
default:
333+
return -EINVAL;
334+
}
335+
336+
addr[0] = spec->data[2] >> 8;
337+
addr[1] = spec->data[2];
338+
addr[2] = spec->data[1] >> 24;
339+
addr[3] = spec->data[1] >> 16;
340+
addr[4] = spec->data[1] >> 8;
341+
addr[5] = spec->data[1];
342+
return 0;
343+
}
344+
273345
/* Build a filter entry and return its n-tuple key. */
274346
static u32 efx_filter_build(efx_oword_t *filter, struct efx_filter_spec *spec)
275347
{
@@ -407,6 +479,20 @@ static inline u8 efx_filter_id_flags(u32 id)
407479
EFX_FILTER_FLAG_RX;
408480
}
409481

482+
u32 efx_filter_get_rx_id_limit(struct efx_nic *efx)
483+
{
484+
struct efx_filter_state *state = efx->filter_state;
485+
486+
if (state->table[EFX_FILTER_TABLE_RX_MAC].size != 0)
487+
return ((EFX_FILTER_TABLE_RX_MAC + 1) << EFX_FILTER_INDEX_WIDTH)
488+
+ state->table[EFX_FILTER_TABLE_RX_MAC].size;
489+
else if (state->table[EFX_FILTER_TABLE_RX_IP].size != 0)
490+
return ((EFX_FILTER_TABLE_RX_IP + 1) << EFX_FILTER_INDEX_WIDTH)
491+
+ state->table[EFX_FILTER_TABLE_RX_IP].size;
492+
else
493+
return 0;
494+
}
495+
410496
/**
411497
* efx_filter_insert_filter - add or replace a filter
412498
* @efx: NIC in which to insert the filter
@@ -495,6 +581,105 @@ static void efx_filter_table_clear_entry(struct efx_nic *efx,
495581
}
496582
}
497583

584+
/**
585+
* efx_filter_remove_id_safe - remove a filter by ID, carefully
586+
* @efx: NIC from which to remove the filter
587+
* @priority: Priority of filter, as passed to @efx_filter_insert_filter
588+
* @filter_id: ID of filter, as returned by @efx_filter_insert_filter
589+
*
590+
* This function will range-check @filter_id, so it is safe to call
591+
* with a value passed from userland.
592+
*/
593+
int efx_filter_remove_id_safe(struct efx_nic *efx,
594+
enum efx_filter_priority priority,
595+
u32 filter_id)
596+
{
597+
struct efx_filter_state *state = efx->filter_state;
598+
enum efx_filter_table_id table_id;
599+
struct efx_filter_table *table;
600+
unsigned int filter_idx;
601+
struct efx_filter_spec *spec;
602+
u8 filter_flags;
603+
int rc;
604+
605+
table_id = efx_filter_id_table_id(filter_id);
606+
if ((unsigned int)table_id >= EFX_FILTER_TABLE_COUNT)
607+
return -ENOENT;
608+
table = &state->table[table_id];
609+
610+
filter_idx = efx_filter_id_index(filter_id);
611+
if (filter_idx >= table->size)
612+
return -ENOENT;
613+
spec = &table->spec[filter_idx];
614+
615+
filter_flags = efx_filter_id_flags(filter_id);
616+
617+
spin_lock_bh(&state->lock);
618+
619+
if (test_bit(filter_idx, table->used_bitmap) &&
620+
spec->priority == priority && spec->flags == filter_flags) {
621+
efx_filter_table_clear_entry(efx, table, filter_idx);
622+
if (table->used == 0)
623+
efx_filter_table_reset_search_depth(table);
624+
rc = 0;
625+
} else {
626+
rc = -ENOENT;
627+
}
628+
629+
spin_unlock_bh(&state->lock);
630+
631+
return rc;
632+
}
633+
634+
/**
635+
* efx_filter_get_filter_safe - retrieve a filter by ID, carefully
636+
* @efx: NIC from which to remove the filter
637+
* @priority: Priority of filter, as passed to @efx_filter_insert_filter
638+
* @filter_id: ID of filter, as returned by @efx_filter_insert_filter
639+
* @spec: Buffer in which to store filter specification
640+
*
641+
* This function will range-check @filter_id, so it is safe to call
642+
* with a value passed from userland.
643+
*/
644+
int efx_filter_get_filter_safe(struct efx_nic *efx,
645+
enum efx_filter_priority priority,
646+
u32 filter_id, struct efx_filter_spec *spec_buf)
647+
{
648+
struct efx_filter_state *state = efx->filter_state;
649+
enum efx_filter_table_id table_id;
650+
struct efx_filter_table *table;
651+
struct efx_filter_spec *spec;
652+
unsigned int filter_idx;
653+
u8 filter_flags;
654+
int rc;
655+
656+
table_id = efx_filter_id_table_id(filter_id);
657+
if ((unsigned int)table_id >= EFX_FILTER_TABLE_COUNT)
658+
return -ENOENT;
659+
table = &state->table[table_id];
660+
661+
filter_idx = efx_filter_id_index(filter_id);
662+
if (filter_idx >= table->size)
663+
return -ENOENT;
664+
spec = &table->spec[filter_idx];
665+
666+
filter_flags = efx_filter_id_flags(filter_id);
667+
668+
spin_lock_bh(&state->lock);
669+
670+
if (test_bit(filter_idx, table->used_bitmap) &&
671+
spec->priority == priority && spec->flags == filter_flags) {
672+
*spec_buf = *spec;
673+
rc = 0;
674+
} else {
675+
rc = -ENOENT;
676+
}
677+
678+
spin_unlock_bh(&state->lock);
679+
680+
return rc;
681+
}
682+
498683
/**
499684
* efx_filter_remove_filter - remove a filter by specification
500685
* @efx: NIC from which to remove the filter
@@ -571,6 +756,68 @@ void efx_filter_clear_rx(struct efx_nic *efx, enum efx_filter_priority priority)
571756
efx_filter_table_clear(efx, EFX_FILTER_TABLE_RX_MAC, priority);
572757
}
573758

759+
u32 efx_filter_count_rx_used(struct efx_nic *efx,
760+
enum efx_filter_priority priority)
761+
{
762+
struct efx_filter_state *state = efx->filter_state;
763+
enum efx_filter_table_id table_id;
764+
struct efx_filter_table *table;
765+
unsigned int filter_idx;
766+
u32 count = 0;
767+
768+
spin_lock_bh(&state->lock);
769+
770+
for (table_id = EFX_FILTER_TABLE_RX_IP;
771+
table_id <= EFX_FILTER_TABLE_RX_MAC;
772+
table_id++) {
773+
table = &state->table[table_id];
774+
for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
775+
if (test_bit(filter_idx, table->used_bitmap) &&
776+
table->spec[filter_idx].priority == priority)
777+
++count;
778+
}
779+
}
780+
781+
spin_unlock_bh(&state->lock);
782+
783+
return count;
784+
}
785+
786+
s32 efx_filter_get_rx_ids(struct efx_nic *efx,
787+
enum efx_filter_priority priority,
788+
u32 *buf, u32 size)
789+
{
790+
struct efx_filter_state *state = efx->filter_state;
791+
enum efx_filter_table_id table_id;
792+
struct efx_filter_table *table;
793+
unsigned int filter_idx;
794+
s32 count = 0;
795+
796+
spin_lock_bh(&state->lock);
797+
798+
for (table_id = EFX_FILTER_TABLE_RX_IP;
799+
table_id <= EFX_FILTER_TABLE_RX_MAC;
800+
table_id++) {
801+
table = &state->table[table_id];
802+
for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
803+
if (test_bit(filter_idx, table->used_bitmap) &&
804+
table->spec[filter_idx].priority == priority) {
805+
if (count == size) {
806+
count = -EMSGSIZE;
807+
goto out;
808+
}
809+
buf[count++] = efx_filter_make_id(
810+
table_id, filter_idx,
811+
table->spec[filter_idx].flags);
812+
}
813+
}
814+
}
815+
out:
816+
spin_unlock_bh(&state->lock);
817+
818+
return count;
819+
}
820+
574821
/* Restore filter stater after reset */
575822
void efx_restore_filters(struct efx_nic *efx)
576823
{

‎drivers/net/ethernet/sfc/filter.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,18 @@ static inline void efx_filter_init_rx(struct efx_filter_spec *spec,
105105

106106
extern int efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
107107
__be32 host, __be16 port);
108+
extern int efx_filter_get_ipv4_local(const struct efx_filter_spec *spec,
109+
u8 *proto, __be32 *host, __be16 *port);
108110
extern int efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
109111
__be32 host, __be16 port,
110112
__be32 rhost, __be16 rport);
113+
extern int efx_filter_get_ipv4_full(const struct efx_filter_spec *spec,
114+
u8 *proto, __be32 *host, __be16 *port,
115+
__be32 *rhost, __be16 *rport);
111116
extern int efx_filter_set_eth_local(struct efx_filter_spec *spec,
112117
u16 vid, const u8 *addr);
118+
extern int efx_filter_get_eth_local(const struct efx_filter_spec *spec,
119+
u16 *vid, u8 *addr);
113120
enum {
114121
EFX_FILTER_VID_UNSPEC = 0xffff,
115122
};

0 commit comments

Comments
 (0)