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