P4C
The P4 Compiler
Loading...
Searching...
No Matches
pna.h
1/*
2 * Copyright (C) 2023 Intel Corporation
3 * SPDX-FileCopyrightText: 2023 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef P4C_PNA_H
9#define P4C_PNA_H
10
11#include <stdbool.h>
12
13// pna.p4 information
14
15typedef __u32 PortId_t;
16typedef __u64 Timestamp_t;
17typedef __u8 ClassOfService_t;
18typedef __u16 CloneSessionId_t;
19typedef __u32 MulticastGroup_t;
20typedef __u16 EgressInstance_t;
21typedef __u8 MirrorSlotId_t;
22typedef __u16 MirrorSessionId_t;
23
24// Instead of using enum we define ParserError_t as __u8 to save memory.
25typedef __u8 ParserError_t;
26static const ParserError_t NoError = 0; // No error.
27static const ParserError_t PacketTooShort = 1; // Not enough bits in packet for 'extract'.
28static const ParserError_t NoMatch = 2; // 'select' expression has no matches.
29static const ParserError_t StackOutOfBounds = 3; // Reference to invalid element of a header stack.
30static const ParserError_t HeaderTooShort = 4; // Extracting too many bits into a varbit field.
31static const ParserError_t ParserTimeout = 5; // Parser execution time limit exceeded.
32static const ParserError_t ParserInvalidArgument = 6; // Parser operation was called with a value
33 // not supported by the implementation
34
35enum PNA_Source_t { FROM_HOST, FROM_NET };
36
37enum PNA_MeterColor_t { RED, GREEN, YELLOW };
38
39enum MirrorType { NO_MIRROR, PRE_MODIFY, POST_MODIFY };
40
42 bool recirculated;
43 PortId_t input_port; // taken from xdp_md or __sk_buff
44} __attribute__((aligned(4)));
45
47 // All of these values are initialized by the architecture before
48 // the control block begins executing.
49 bool recirculated;
50 Timestamp_t timestamp; // taken from bpf helper
51 ParserError_t parser_error; // local to parser
52 ClassOfService_t class_of_service; // 0, set in control as global metadata
53 PortId_t input_port;
54} __attribute__((aligned(4)));;
55
57 // The comment after each field specifies its initial value when the
58 // control block begins executing.
59 ClassOfService_t class_of_service;
60} __attribute__((aligned(4)));
61
62/*
63 * Opaque struct to be used to share global PNA metadata fields.
64 * The size of this struct must be less than 32 bytes.
65 */
67 bool recirculated;
68 bool recirculate;
69 bool drop; // NOTE : no drop field in PNA metadata, so we keep drop state as internal metadata.
70 PortId_t egress_port;
71 enum MirrorType mirror_type;
72 MirrorSlotId_t mirror_slot_id;
73 ParserError_t parser_error;
74 MirrorSessionId_t mirror_session_id;
75 __u8 mark;
76 bool pass_to_kernel; // internal metadata, forces sending packet up to kernel stack
77} __attribute__((aligned(4)));
78
80 __u32 egress_port;
81 __u16 instance;
82 __u8 class_of_service;
83 __u8 truncate;
84 __u16 packet_length_bytes;
85} __attribute__((aligned(4)));
86
87#define send_to_port(x) (compiler_meta__->egress_port = x)
88#define drop_packet() (compiler_meta__->drop = true)
89
90// structures and functions for tc backend
91
93 u32 pipeid;
94 u32 tblid;
95};
96
97struct __attribute__((__packed__)) p4tc_table_entry_act_bpf {
98 u32 act_id;
99 u32 hit:1,
100 is_default_miss_act:1,
101 is_default_hit_act:1;
102 u8 params[124];
103};
104
106 struct p4tc_table_entry_act_bpf act_bpf;
107 u32 profile_id;
108 u32 pipeid;
109 u32 tblid;
110 u32 handle;
111 u32 chain;
112 u32 classid;
113 u16 proto;
114 u16 prio;
115};
116extern struct p4tc_table_entry_act_bpf *
117bpf_p4tc_tbl_read(struct __sk_buff *skb_ctx,
119 const u32 params__sz,
120 void *key, const __u32 key__sz) __ksym;
121
122extern struct p4tc_table_entry_act_bpf *
123xdp_p4tc_tbl_read(struct xdp_md *skb_ctx,
125 const u32 params__sz,
126 void *key, const __u32 key__sz) __ksym;
127
128/* No mapping to PNA, but are useful utilities */
129extern int
130bpf_p4tc_entry_create(struct __sk_buff *skb_ctx,
132 const u32 params__sz,
133 void *key, const u32 key__sz) __ksym;
134
135extern int
136xdp_p4tc_entry_create(struct xdp_md *xdp_ctx,
138 const u32 params__sz,
139 void *bpf_key_mask, u32 bpf_key_mask__sz) __ksym;
140
141/* Equivalent to PNA add-on-miss */
142extern int
143bpf_p4tc_entry_create_on_miss(struct __sk_buff *skb_ctx,
145 const u32 params__sz,
146 void *key, const u32 key__sz) __ksym;
147
148extern int
149xdp_p4tc_entry_create_on_miss(struct xdp_md *xdp_ctx,
151 const u32 params__sz,
152 void *key, const u32 key__sz) __ksym;
153
154/* No mapping to PNA, but are useful utilities */
155extern int
156bpf_p4tc_entry_update(struct __sk_buff *skb_ctx,
158 const u32 params__sz,
159 void *key, const u32 key__sz) __ksym;
160
161extern int
162xdp_p4tc_entry_update(struct xdp_md *xdp_ctx,
164 const u32 params__sz,
165 void *key, const u32 key__sz) __ksym;
166
167/* No mapping to PNA, but are useful utilities */
168extern int
169bpf_p4tc_entry_delete(struct __sk_buff *skb_ctx,
171 const u32 params__sz,
172 void *key, const u32 key__sz) __ksym;
173
174extern int
175xdp_p4tc_entry_delete(struct xdp_md *xdp_ctx,
177 const u32 params__sz,
178 void *key, const u32 key__sz) __ksym;
179
180/* Start generic kfunc interface to any extern */
181
182#define P4TC_EXT_CNT_DIRECT 0x1
183#define P4TC_EXT_CNT_INDIRECT 0x2
184#define P4TC_EXT_METER_DIRECT (1 << 2)
185#define P4TC_EXT_METER_INDIRECT (1 << 3)
186
188 u32 pipe_id;
189 u32 ext_id;
190 u32 inst_id;
191 u32 tbl_id;
192 u32 index;
193 u32 flags;
194 u8 in_params[64]; /* extern specific params if any */
195};
196
198 u32 ext_id;
199 u32 index_id;
200 u32 verdict;
201 u8 out_params[64]; /* specific values if any */
202};
203
204/* Equivalent to PNA indirect counters */
205extern int
206bpf_p4tc_extern_count_pktsnbytes(struct __sk_buff *skb_ctx,
207 struct p4tc_ext_bpf_params *params,
208 const u32 params__sz, void *key, const u32 key__sz) __ksym;
209
210extern int
211bpf_p4tc_extern_count_pkts(struct __sk_buff *skb_ctx,
212 struct p4tc_ext_bpf_params *params,
213 const u32 params__sz, void *key, const u32 key__sz) __ksym;
214
215extern int
216bpf_p4tc_extern_count_bytes(struct __sk_buff *skb_ctx,
217 struct p4tc_ext_bpf_params *params,
218 const u32 params__sz, void *key, const u32 key__sz) __ksym;
219
220extern int
221xdp_p4tc_extern_count_pktsnbytes(struct xdp_md *xdp_ctx,
222 struct p4tc_ext_bpf_params *params,
223 const u32 params__sz) __ksym;
224
225extern int
226xdp_p4tc_extern_count_pkts(struct xdp_md *xdp_ctx,
227 struct p4tc_ext_bpf_params *params,
228 const u32 params__sz) __ksym;
229
230extern int
231xdp_p4tc_extern_count_bytes(struct xdp_md *xdp_ctx,
232 struct p4tc_ext_bpf_params *params,
233 const u32 params__sz) __ksym;
234
235extern int bpf_p4tc_extern_meter_bytes_color(struct __sk_buff *skb_ctx,
236 struct p4tc_ext_bpf_params *params,
237 const u32 params__sz, void *key,
238 const u32 key__sz) __ksym;
239extern int
240bpf_p4tc_extern_meter_bytes(struct __sk_buff *skb,
241 struct p4tc_ext_bpf_params *params,
242 const u32 params__sz, void *key,
243 const u32 key__sz) __ksym;
244
245extern int bpf_p4tc_extern_meter_pkts_color(struct __sk_buff *skb_ctx,
246 struct p4tc_ext_bpf_params *params,
247 const u32 params__sz, void *key,
248 const u32 key__sz) __ksym;
249extern int
250bpf_p4tc_extern_meter_pkts(struct __sk_buff *skb,
251 struct p4tc_ext_bpf_params *params,
252 const u32 params__sz, void *key,
253 const u32 key__sz) __ksym;
254
255extern int xdp_p4tc_extern_meter_bytes_color(struct xdp_md *xdp_ctx,
256 struct p4tc_ext_bpf_params *params,
257 const u32 params__sz, void *key,
258 const u32 key__sz) __ksym;
259
260extern int xdp_p4tc_extern_meter_bytes(struct xdp_md *xdp_ctx,
261 struct p4tc_ext_bpf_params *params,
262 const u32 params__sz, void *key,
263 const u32 key__sz) __ksym;
264
265extern int xdp_p4tc_extern_meter_pkts_color(struct xdp_md *xdp_ctx,
266 struct p4tc_ext_bpf_params *params,
267 const u32 params__sz, void *key,
268 const u32 key__sz) __ksym;
269
270extern int xdp_p4tc_extern_meter_pkts(struct xdp_md *xdp_ctx,
271 struct p4tc_ext_bpf_params *params,
272 const u32 params__sz, void *key,
273 const u32 key__sz) __ksym;
274
275/* Start checksum related kfuncs */
277 __wsum csum;
278};
279
280/* Equivalent to PNA CRC16 checksum */
281/* Basic checksums are not implemented in DPDK */
282extern u16
283bpf_p4tc_ext_csum_crc16_add(struct p4tc_ext_csum_params *params,
284 const u32 params__sz, const void *data,
285 const u32 data__sz) __ksym;
286
287extern u16
288bpf_p4tc_ext_csum_crc16_get(struct p4tc_ext_csum_params *params,
289 const u32 params__sz) __ksym;
290
291extern void
292bpf_p4tc_ext_csum_crc16_clear(struct p4tc_ext_csum_params *params,
293 const u32 params__sz) __ksym;
294
295/* Equivalent to PNA CRC32 checksum */
296/* Basic checksums are not implemented in DPDK */
297extern u32
298bpf_p4tc_ext_csum_crc32_add(struct p4tc_ext_csum_params *params,
299 const u32 params__sz,
300 const void *data, const u32 data__sz) __ksym;
301
302extern u32
303bpf_p4tc_ext_csum_crc32_get(struct p4tc_ext_csum_params *params,
304 const u32 params__sz) __ksym;
305
306extern void
307bpf_p4tc_ext_csum_crc32_clear(struct p4tc_ext_csum_params *params,
308 const u32 params__sz) __ksym;
309
310extern u16
311bpf_p4tc_ext_csum_16bit_complement_get(struct p4tc_ext_csum_params *params,
312 const u32 params__sz) __ksym;
313
314/* Equivalent to PNA 16bit complement checksum (incremental checksum) */
315extern __wsum
316bpf_p4tc_ext_csum_16bit_complement_add(struct p4tc_ext_csum_params *params,
317 const u32 params__sz,
318 const void *data, int len) __ksym;
319
320extern int
321bpf_p4tc_ext_csum_16bit_complement_sub(struct p4tc_ext_csum_params *params,
322 const u32 params__sz,
323 const void *data, const u32 data__sz) __ksym;
324
325extern void
326bpf_p4tc_ext_csum_16bit_complement_clear(struct p4tc_ext_csum_params *params,
327 const u32 params__sz) __ksym;
328
329extern void
330bpf_p4tc_ext_csum_16bit_complement_set_state(struct p4tc_ext_csum_params *params,
331 const u32 params__sz,
332 u16 csum) __ksym;
333
334/* Equivalent to PNA crc16 hash */
335extern u16
336bpf_p4tc_ext_hash_crc16(const void *data, int len, u16 seed) __ksym;
337
338/* Equivalent to PNA crc16 hash base */
339static inline u16
340bpf_p4tc_ext_hash_base_crc16(const void *data, const u32 data__sz,
341 u32 base, u32 max, u16 seed) {
342 u16 hash = bpf_p4tc_ext_hash_crc16(data, data__sz, seed);
343
344 return (base + (hash % max));
345}
346
347/* Equivalent to PNA crc32 hash */
348extern u32
349bpf_p4tc_ext_hash_crc32(const void *data, const u32 data__sz, u32 seed) __ksym;
350
351/* Equivalent to PNA crc32 hash base */
352static inline u32
353bpf_p4tc_ext_hash_base_crc32(const void *data, const u32 data__sz,
354 u32 base, u32 max, u32 seed) {
355 u32 hash = bpf_p4tc_ext_hash_crc32(data, data__sz, seed);
356
357 return (base + (hash % max));
358}
359
360/* Equivalent to PNA 16-bit ones complement hash */
361extern u16
362bpf_p4tc_ext_hash_16bit_complement(const void *data, const u32 data__sz,
363 u16 seed) __ksym;
364
365/* Equivalent to PNA 16-bit ones complement hash base */
366static inline u16
367bpf_p4tc_ext_hash_base_16bit_complement(const void *data, const u32 data__sz,
368 u32 base, u32 max, u16 seed) {
369 u16 hash = bpf_p4tc_ext_hash_16bit_complement(data, data__sz, seed);
370
371 return (base + (hash % max));
372}
373
374/* DPDK doesn't implement this yet */
375/* Equivalent to PNA is_net_port */
376extern bool
377bpf_p4tc_is_net_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
378
379/* DPDK doesn't implement this yet */
380/* Equivalent to PNA is_host_port */
381extern bool
382bpf_p4tc_is_host_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
383
384/* DPDK doesn't implement this yet */
385/* Equivalent to PNA is_net_port */
386extern bool
387xdp_p4tc_is_net_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
388
389/* DPDK doesn't implement this yet */
390/* Equivalent to PNA is_host_port */
391extern bool
392xdp_p4tc_is_host_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
393
394
395/* per extern specifics start */
396
397/* in this case it is PNA so we have these helpers like below
398 "is_net_port_skb" but for user specific externs the caller should
399 be doing similar flow:
400 a) populating p4tc_ext_bpf_params struct with their proper
401 parametrization of pipeline id, extern id, and extern specific params
402 b) receiving a response and retrieving their extern-specific data/return
403 codes from res->params
404*/
405
406#define EXTERN_IS_NET_PORT 1234
407#define EXTERN_IS_HOST_PORT 4567
408
409/* Extern control path read (for example, used for register read) */
410extern struct p4tc_ext_bpf_val *
411bpf_p4tc_extern_md_read(struct __sk_buff *skb_ctx,
412 struct p4tc_ext_bpf_params *params,
413 const u32 params__sz) __ksym;
414
415/* Extern control path write (for example, used for register write) */
416extern int bpf_p4tc_extern_md_write(struct __sk_buff *skb_ctx,
417 struct p4tc_ext_bpf_params *params,
418 const u32 params__sz,
419 struct p4tc_ext_bpf_val *val,
420 const u32 val__sz) __ksym;
421
422/* Extern control path read (for example, used for register read) for XDP */
423extern struct p4tc_ext_bpf_val *
424xdp_p4tc_extern_md_read(struct xdp_md *xdp_ctx,
425 struct p4tc_ext_bpf_params *params,
426 const u32 params__sz) __ksym;
427
428/* Extern control path read (for example, used for register write for XDP */
429extern int xdp_p4tc_extern_md_write(struct xdp_md *xdp_ctx,
430 struct p4tc_ext_bpf_params *params,
431 const u32 params__sz,
432 struct p4tc_ext_bpf_val *val,
433 const u32 val__sz) __ksym;
434
435int bpf_p4tc_extern_digest_pack(struct __sk_buff *skb,
436 struct p4tc_ext_bpf_params *params,
437 const u32 params__sz) __ksym;
438
439int xdp_p4tc_extern_digest_pack(struct xdp_md *xdp_ctx,
440 struct p4tc_ext_bpf_params *params,
441 const u32 params__sz) __ksym;
442
443/* Timestamp PNA extern */
444static inline u64 bpf_p4tc_extern_timestamp() {
445 return bpf_ktime_get_ns();
446}
447
448#define BIT(x) (1 << x)
449
450#define P4TC_SKB_META_SET_TSTAMP BIT(0)
451#define P4TC_SKB_META_SET_MARK BIT(1)
452#define P4TC_SKB_META_SET_CLASSID BIT(2)
453#define P4TC_SKB_META_SET_TC_INDEX BIT(3)
454#define P4TC_SKB_META_SET_QMAP BIT(4)
455#define P4TC_SKB_META_SET_PROTO BIT(5)
456
458 __u64 tstamp;
459 __u32 mark;
460 __u16 tc_classid;
461 __u16 tc_index;
462 __u16 queue_mapping;
463 __be16 protocol;
464 __u32 bitmask;
465};
466
467static inline void
468bpf_p4tc_skb_set_tstamp(struct __sk_buff *skb,
469 struct p4tc_skb_meta_set *meta_set, __u64 tstamp)
470{
471 meta_set->tstamp = tstamp;
472 meta_set->bitmask |= P4TC_SKB_META_SET_TSTAMP;
473}
474
475static inline void
476bpf_p4tc_skb_set_mark(struct __sk_buff *skb,
477 struct p4tc_skb_meta_set *meta_set, __u32 mark)
478{
479 meta_set->mark = mark;
480 meta_set->bitmask |= P4TC_SKB_META_SET_MARK;
481}
482
483static inline void
484bpf_p4tc_skb_set_tc_classid(struct __sk_buff *skb,
485 struct p4tc_skb_meta_set *meta_set, __u32 tc_classid)
486{
487 meta_set->tc_classid = tc_classid;
488 meta_set->bitmask |= P4TC_SKB_META_SET_CLASSID;
489}
490
491static inline void
492bpf_p4tc_skb_set_tc_index(struct __sk_buff *skb,
493 struct p4tc_skb_meta_set *meta_set, __u16 tc_index)
494{
495 meta_set->tc_index = tc_index;
496 meta_set->bitmask |= P4TC_SKB_META_SET_TC_INDEX;
497}
498
499static inline void
500bpf_p4tc_skb_set_queue_mapping(struct __sk_buff *skb,
501 struct p4tc_skb_meta_set *meta_set,
502 __u16 queue_mapping)
503{
504 meta_set->queue_mapping = queue_mapping;
505 meta_set->bitmask |= P4TC_SKB_META_SET_QMAP;
506}
507
508static inline void
509bpf_p4tc_skb_set_protocol(struct __sk_buff *skb,
510 struct p4tc_skb_meta_set *meta_set, __be16 protocol)
511{
512 meta_set->protocol = protocol;
513 meta_set->bitmask |= P4TC_SKB_META_SET_PROTO;
514}
515
516int bpf_p4tc_skb_meta_set(struct __sk_buff *skb,
517 struct p4tc_skb_meta_set *skb_meta_set,
518 u32 skb_meta_set__sz) __ksym;
519
520#define P4TC_SKB_META_GET_AT_INGRESS_BIT BIT(0)
521#define P4TC_SKB_META_GET_FROM_INGRESS_BIT BIT(1)
522
524 u8 tc_at_ingress:1,
525 from_ingress:1;
526 u8 bitmask;
527};
528
529static inline __u64
530bpf_p4tc_skb_get_tstamp(struct __sk_buff *skb,
531 struct p4tc_skb_meta_get *meta_get)
532{
533 return skb->tstamp;
534}
535
536static inline __u16
537bpf_p4tc_skb_get_tc_classid(struct __sk_buff *skb,
538 struct p4tc_skb_meta_get *meta_get)
539{
540 return skb->tc_classid;
541}
542
543static inline __u16
544bpf_p4tc_skb_get_tc_index(struct __sk_buff *skb,
545 struct p4tc_skb_meta_get *meta_get)
546{
547 return skb->tc_index;
548}
549
550static inline __u16
551bpf_p4tc_skb_get_queue_mapping(struct __sk_buff *skb,
552 struct p4tc_skb_meta_get *meta_get)
553{
554 return skb->queue_mapping;
555}
556
557static inline __be16
558bpf_p4tc_skb_get_protocol(struct __sk_buff *skb,
559 struct p4tc_skb_meta_get *meta_get)
560{
561 return skb->protocol;
562}
563
564static inline int
565bpf_p4tc_skb_get_tc_at_ingress(struct __sk_buff *skb,
566 struct p4tc_skb_meta_get *meta_get)
567{
568 if (meta_get->bitmask & P4TC_SKB_META_GET_AT_INGRESS_BIT)
569 return meta_get->tc_at_ingress;
570
571 return -1;
572}
573
574static inline int
575bpf_p4tc_skb_get_from_ingress(struct __sk_buff *skb,
576 struct p4tc_skb_meta_get *meta_get)
577{
578 if (meta_get->bitmask & P4TC_SKB_META_GET_FROM_INGRESS_BIT)
579 return meta_get->from_ingress;
580
581 return -1;
582}
583
584static inline __u32
585bpf_p4tc_skb_get_mark(struct __sk_buff *skb,
586 struct p4tc_skb_meta_get *meta_get)
587{
588 return skb->mark;
589}
590
591int bpf_p4tc_skb_meta_get(struct __sk_buff *skb,
592 struct p4tc_skb_meta_get *skb_meta_get,
593 u32 skb_meta_get__sz) __ksym;
594
595#endif /* P4C_PNA_H */
Definition ebpf/runtime/psa.h:114
Definition pna.h:187
Definition pna.h:197
Definition pna.h:276
Definition pna.h:523
Definition pna.h:457
Definition pna.h:92
Definition pna.h:105
Definition pna.h:66
Definition pna.h:46
Definition pna.h:56