Skip to content

Commit ff3d43f

Browse files
Jakub Kicinskiborkmann
authored andcommitted
nfp: bpf: implement helpers for FW map ops
Implement calls for FW map communication. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent d48ae23 commit ff3d43f

File tree

3 files changed

+288
-4
lines changed

3 files changed

+288
-4
lines changed

drivers/net/ethernet/netronome/nfp/bpf/cmsg.c

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* SOFTWARE.
3232
*/
3333

34+
#include <linux/bpf.h>
3435
#include <linux/bitops.h>
3536
#include <linux/bug.h>
3637
#include <linux/jiffies.h>
@@ -79,6 +80,28 @@ static void nfp_bpf_free_tag(struct nfp_app_bpf *bpf, u16 tag)
7980
bpf->tag_alloc_last++;
8081
}
8182

83+
static struct sk_buff *
84+
nfp_bpf_cmsg_alloc(struct nfp_app_bpf *bpf, unsigned int size)
85+
{
86+
struct sk_buff *skb;
87+
88+
skb = nfp_app_ctrl_msg_alloc(bpf->app, size, GFP_KERNEL);
89+
skb_put(skb, size);
90+
91+
return skb;
92+
}
93+
94+
static struct sk_buff *
95+
nfp_bpf_cmsg_map_req_alloc(struct nfp_app_bpf *bpf, unsigned int n)
96+
{
97+
unsigned int size;
98+
99+
size = sizeof(struct cmsg_req_map_op);
100+
size += sizeof(struct cmsg_key_value_pair) * n;
101+
102+
return nfp_bpf_cmsg_alloc(bpf, size);
103+
}
104+
82105
static unsigned int nfp_bpf_cmsg_get_tag(struct sk_buff *skb)
83106
{
84107
struct cmsg_hdr *hdr;
@@ -159,7 +182,7 @@ nfp_bpf_cmsg_wait_reply(struct nfp_app_bpf *bpf, enum nfp_bpf_cmsg_type type,
159182
return skb;
160183
}
161184

162-
struct sk_buff *
185+
static struct sk_buff *
163186
nfp_bpf_cmsg_communicate(struct nfp_app_bpf *bpf, struct sk_buff *skb,
164187
enum nfp_bpf_cmsg_type type, unsigned int reply_size)
165188
{
@@ -206,6 +229,191 @@ nfp_bpf_cmsg_communicate(struct nfp_app_bpf *bpf, struct sk_buff *skb,
206229
return ERR_PTR(-EIO);
207230
}
208231

232+
static int
233+
nfp_bpf_ctrl_rc_to_errno(struct nfp_app_bpf *bpf,
234+
struct cmsg_reply_map_simple *reply)
235+
{
236+
static const int res_table[] = {
237+
[CMSG_RC_SUCCESS] = 0,
238+
[CMSG_RC_ERR_MAP_FD] = -EBADFD,
239+
[CMSG_RC_ERR_MAP_NOENT] = -ENOENT,
240+
[CMSG_RC_ERR_MAP_ERR] = -EINVAL,
241+
[CMSG_RC_ERR_MAP_PARSE] = -EIO,
242+
[CMSG_RC_ERR_MAP_EXIST] = -EEXIST,
243+
[CMSG_RC_ERR_MAP_NOMEM] = -ENOMEM,
244+
[CMSG_RC_ERR_MAP_E2BIG] = -E2BIG,
245+
};
246+
u32 rc;
247+
248+
rc = be32_to_cpu(reply->rc);
249+
if (rc >= ARRAY_SIZE(res_table)) {
250+
cmsg_warn(bpf, "FW responded with invalid status: %u\n", rc);
251+
return -EIO;
252+
}
253+
254+
return res_table[rc];
255+
}
256+
257+
long long int
258+
nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map)
259+
{
260+
struct cmsg_reply_map_alloc_tbl *reply;
261+
struct cmsg_req_map_alloc_tbl *req;
262+
struct sk_buff *skb;
263+
u32 tid;
264+
int err;
265+
266+
skb = nfp_bpf_cmsg_alloc(bpf, sizeof(*req));
267+
if (!skb)
268+
return -ENOMEM;
269+
270+
req = (void *)skb->data;
271+
req->key_size = cpu_to_be32(map->key_size);
272+
req->value_size = cpu_to_be32(map->value_size);
273+
req->max_entries = cpu_to_be32(map->max_entries);
274+
req->map_type = cpu_to_be32(map->map_type);
275+
req->map_flags = 0;
276+
277+
skb = nfp_bpf_cmsg_communicate(bpf, skb, CMSG_TYPE_MAP_ALLOC,
278+
sizeof(*reply));
279+
if (IS_ERR(skb))
280+
return PTR_ERR(skb);
281+
282+
reply = (void *)skb->data;
283+
err = nfp_bpf_ctrl_rc_to_errno(bpf, &reply->reply_hdr);
284+
if (err)
285+
goto err_free;
286+
287+
tid = be32_to_cpu(reply->tid);
288+
dev_consume_skb_any(skb);
289+
290+
return tid;
291+
err_free:
292+
dev_kfree_skb_any(skb);
293+
return err;
294+
}
295+
296+
void nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map)
297+
{
298+
struct cmsg_reply_map_free_tbl *reply;
299+
struct cmsg_req_map_free_tbl *req;
300+
struct sk_buff *skb;
301+
int err;
302+
303+
skb = nfp_bpf_cmsg_alloc(bpf, sizeof(*req));
304+
if (!skb) {
305+
cmsg_warn(bpf, "leaking map - failed to allocate msg\n");
306+
return;
307+
}
308+
309+
req = (void *)skb->data;
310+
req->tid = cpu_to_be32(nfp_map->tid);
311+
312+
skb = nfp_bpf_cmsg_communicate(bpf, skb, CMSG_TYPE_MAP_FREE,
313+
sizeof(*reply));
314+
if (IS_ERR(skb)) {
315+
cmsg_warn(bpf, "leaking map - I/O error\n");
316+
return;
317+
}
318+
319+
reply = (void *)skb->data;
320+
err = nfp_bpf_ctrl_rc_to_errno(bpf, &reply->reply_hdr);
321+
if (err)
322+
cmsg_warn(bpf, "leaking map - FW responded with: %d\n", err);
323+
324+
dev_consume_skb_any(skb);
325+
}
326+
327+
static int
328+
nfp_bpf_ctrl_entry_op(struct bpf_offloaded_map *offmap,
329+
enum nfp_bpf_cmsg_type op,
330+
u8 *key, u8 *value, u64 flags, u8 *out_key, u8 *out_value)
331+
{
332+
struct nfp_bpf_map *nfp_map = offmap->dev_priv;
333+
struct nfp_app_bpf *bpf = nfp_map->bpf;
334+
struct bpf_map *map = &offmap->map;
335+
struct cmsg_reply_map_op *reply;
336+
struct cmsg_req_map_op *req;
337+
struct sk_buff *skb;
338+
int err;
339+
340+
/* FW messages have no space for more than 32 bits of flags */
341+
if (flags >> 32)
342+
return -EOPNOTSUPP;
343+
344+
skb = nfp_bpf_cmsg_map_req_alloc(bpf, 1);
345+
if (!skb)
346+
return -ENOMEM;
347+
348+
req = (void *)skb->data;
349+
req->tid = cpu_to_be32(nfp_map->tid);
350+
req->count = cpu_to_be32(1);
351+
req->flags = cpu_to_be32(flags);
352+
353+
/* Copy inputs */
354+
if (key)
355+
memcpy(&req->elem[0].key, key, map->key_size);
356+
if (value)
357+
memcpy(&req->elem[0].value, value, map->value_size);
358+
359+
skb = nfp_bpf_cmsg_communicate(bpf, skb, op,
360+
sizeof(*reply) + sizeof(*reply->elem));
361+
if (IS_ERR(skb))
362+
return PTR_ERR(skb);
363+
364+
reply = (void *)skb->data;
365+
err = nfp_bpf_ctrl_rc_to_errno(bpf, &reply->reply_hdr);
366+
if (err)
367+
goto err_free;
368+
369+
/* Copy outputs */
370+
if (out_key)
371+
memcpy(out_key, &reply->elem[0].key, map->key_size);
372+
if (out_value)
373+
memcpy(out_value, &reply->elem[0].value, map->value_size);
374+
375+
dev_consume_skb_any(skb);
376+
377+
return 0;
378+
err_free:
379+
dev_kfree_skb_any(skb);
380+
return err;
381+
}
382+
383+
int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
384+
void *key, void *value, u64 flags)
385+
{
386+
return nfp_bpf_ctrl_entry_op(offmap, CMSG_TYPE_MAP_UPDATE,
387+
key, value, flags, NULL, NULL);
388+
}
389+
390+
int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key)
391+
{
392+
return nfp_bpf_ctrl_entry_op(offmap, CMSG_TYPE_MAP_DELETE,
393+
key, NULL, 0, NULL, NULL);
394+
}
395+
396+
int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
397+
void *key, void *value)
398+
{
399+
return nfp_bpf_ctrl_entry_op(offmap, CMSG_TYPE_MAP_LOOKUP,
400+
key, NULL, 0, NULL, value);
401+
}
402+
403+
int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
404+
void *next_key)
405+
{
406+
return nfp_bpf_ctrl_entry_op(offmap, CMSG_TYPE_MAP_GETFIRST,
407+
NULL, NULL, 0, next_key, NULL);
408+
}
409+
410+
int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
411+
void *key, void *next_key)
412+
{
413+
return nfp_bpf_ctrl_entry_op(offmap, CMSG_TYPE_MAP_GETNEXT,
414+
key, NULL, 0, next_key, NULL);
415+
}
416+
209417
void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb)
210418
{
211419
struct nfp_app_bpf *bpf = app->priv;

drivers/net/ethernet/netronome/nfp/bpf/fw.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,33 @@ struct nfp_bpf_cap_tlv_adjust_head {
5757
#define CMSG_MAP_ABI_VERSION 1
5858

5959
enum nfp_bpf_cmsg_type {
60+
CMSG_TYPE_MAP_ALLOC = 1,
61+
CMSG_TYPE_MAP_FREE = 2,
62+
CMSG_TYPE_MAP_LOOKUP = 3,
63+
CMSG_TYPE_MAP_UPDATE = 4,
64+
CMSG_TYPE_MAP_DELETE = 5,
65+
CMSG_TYPE_MAP_GETNEXT = 6,
66+
CMSG_TYPE_MAP_GETFIRST = 7,
6067
__CMSG_TYPE_MAP_MAX,
6168
};
6269

6370
#define CMSG_TYPE_MAP_REPLY_BIT 7
6471
#define __CMSG_REPLY(req) (BIT(CMSG_TYPE_MAP_REPLY_BIT) | (req))
6572

73+
#define CMSG_MAP_KEY_LW 16
74+
#define CMSG_MAP_VALUE_LW 16
75+
76+
enum nfp_bpf_cmsg_status {
77+
CMSG_RC_SUCCESS = 0,
78+
CMSG_RC_ERR_MAP_FD = 1,
79+
CMSG_RC_ERR_MAP_NOENT = 2,
80+
CMSG_RC_ERR_MAP_ERR = 3,
81+
CMSG_RC_ERR_MAP_PARSE = 4,
82+
CMSG_RC_ERR_MAP_EXIST = 5,
83+
CMSG_RC_ERR_MAP_NOMEM = 6,
84+
CMSG_RC_ERR_MAP_E2BIG = 7,
85+
};
86+
6687
struct cmsg_hdr {
6788
u8 type;
6889
u8 ver;
@@ -73,4 +94,48 @@ struct cmsg_reply_map_simple {
7394
struct cmsg_hdr hdr;
7495
__be32 rc;
7596
};
97+
98+
struct cmsg_req_map_alloc_tbl {
99+
struct cmsg_hdr hdr;
100+
__be32 key_size; /* in bytes */
101+
__be32 value_size; /* in bytes */
102+
__be32 max_entries;
103+
__be32 map_type;
104+
__be32 map_flags; /* reserved */
105+
};
106+
107+
struct cmsg_reply_map_alloc_tbl {
108+
struct cmsg_reply_map_simple reply_hdr;
109+
__be32 tid;
110+
};
111+
112+
struct cmsg_req_map_free_tbl {
113+
struct cmsg_hdr hdr;
114+
__be32 tid;
115+
};
116+
117+
struct cmsg_reply_map_free_tbl {
118+
struct cmsg_reply_map_simple reply_hdr;
119+
__be32 count;
120+
};
121+
122+
struct cmsg_key_value_pair {
123+
__be32 key[CMSG_MAP_KEY_LW];
124+
__be32 value[CMSG_MAP_VALUE_LW];
125+
};
126+
127+
struct cmsg_req_map_op {
128+
struct cmsg_hdr hdr;
129+
__be32 tid;
130+
__be32 count;
131+
__be32 flags;
132+
struct cmsg_key_value_pair elem[0];
133+
};
134+
135+
struct cmsg_reply_map_op {
136+
struct cmsg_reply_map_simple reply_hdr;
137+
__be32 count;
138+
__be32 resv;
139+
struct cmsg_key_value_pair elem[0];
140+
};
76141
#endif

drivers/net/ethernet/netronome/nfp/bpf/main.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,19 @@ nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
303303

304304
void *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
305305

306-
struct sk_buff *
307-
nfp_bpf_cmsg_communicate(struct nfp_app_bpf *bpf, struct sk_buff *skb,
308-
enum nfp_bpf_cmsg_type type, unsigned int reply_size);
306+
long long int
307+
nfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
308+
void
309+
nfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map);
310+
int nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
311+
void *next_key);
312+
int nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
313+
void *key, void *value, u64 flags);
314+
int nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key);
315+
int nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
316+
void *key, void *value);
317+
int nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
318+
void *key, void *next_key);
319+
309320
void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb);
310321
#endif

0 commit comments

Comments
 (0)