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