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 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 pipeid;
117 u32 tblid;
118 u32 profile_id;
119 u32 handle;
120 u32 classid;
121 u32 chain;
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 void *data, const u32 data__sz) __ksym;
294
295extern u16
296bpf_p4tc_ext_csum_crc16_get(struct p4tc_ext_csum_params *params) __ksym;
297
298extern void
299bpf_p4tc_ext_csum_crc16_clear(struct p4tc_ext_csum_params *params) __ksym;
300
301/* Equivalent to PNA CRC32 checksum */
302/* Basic checksums are not implemented in DPDK */
303extern u32
304bpf_p4tc_ext_csum_crc32_add(struct p4tc_ext_csum_params *params,
305 const u32 params__sz,
306 const void *data, const u32 data__sz) __ksym;
307
308extern u32
309bpf_p4tc_ext_csum_crc32_get(struct p4tc_ext_csum_params *params,
310 const u32 params__sz) __ksym;
311
312extern void
313bpf_p4tc_ext_csum_crc32_clear(struct p4tc_ext_csum_params *params,
314 const u32 params__sz) __ksym;
315
316extern u16
317bpf_p4tc_ext_csum_16bit_complement_get(struct p4tc_ext_csum_params *params,
318 const u32 params__sz) __ksym;
319
320/* Equivalent to PNA 16bit complement checksum (incremental checksum) */
321extern __wsum
322bpf_p4tc_ext_csum_16bit_complement_add(struct p4tc_ext_csum_params *params,
323 const u32 params__sz,
324 const void *data, int len) __ksym;
325
326extern int
327bpf_p4tc_ext_csum_16bit_complement_sub(struct p4tc_ext_csum_params *params,
328 const u32 params__sz,
329 const void *data, const u32 data__sz) __ksym;
330
331extern void
332bpf_p4tc_ext_csum_16bit_complement_clear(struct p4tc_ext_csum_params *params,
333 const u32 params__sz) __ksym;
334
335extern void
336bpf_p4tc_ext_csum_16bit_complement_set_state(struct p4tc_ext_csum_params *params,
337 const u32 params__sz,
338 u16 csum) __ksym;
339
340/* Equivalent to PNA crc16 hash */
341extern u16
342bpf_p4tc_ext_hash_crc16(const void *data, int len, u16 seed) __ksym;
343
344/* Equivalent to PNA crc16 hash base */
345static inline u16
346bpf_p4tc_ext_hash_base_crc16(const void *data, const u32 data__sz,
347 u32 base, u32 max, u16 seed) {
348 u16 hash = bpf_p4tc_ext_hash_crc16(data, data__sz, seed);
349
350 return (base + (hash % max));
351}
352
353/* Equivalent to PNA crc32 hash */
354extern u32
355bpf_p4tc_ext_hash_crc32(const void *data, const u32 data__sz, u32 seed) __ksym;
356
357/* Equivalent to PNA crc32 hash base */
358static inline u32
359bpf_p4tc_ext_hash_base_crc32(const void *data, const u32 data__sz,
360 u32 base, u32 max, u32 seed) {
361 u32 hash = bpf_p4tc_ext_hash_crc32(data, data__sz, seed);
362
363 return (base + (hash % max));
364}
365
366/* Equivalent to PNA 16-bit ones complement hash */
367extern u16
368bpf_p4tc_ext_hash_16bit_complement(const void *data, const u32 data__sz,
369 u16 seed) __ksym;
370
371/* Equivalent to PNA 16-bit ones complement hash base */
372static inline u16
373bpf_p4tc_ext_hash_base_16bit_complement(const void *data, const u32 data__sz,
374 u32 base, u32 max, u16 seed) {
375 u16 hash = bpf_p4tc_ext_hash_16bit_complement(data, data__sz, seed);
376
377 return (base + (hash % max));
378}
379
380/* DPDK doesn't implement this yet */
381/* Equivalent to PNA is_net_port */
382extern bool
383bpf_p4tc_is_net_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
384
385/* DPDK doesn't implement this yet */
386/* Equivalent to PNA is_host_port */
387extern bool
388bpf_p4tc_is_host_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
389
390/* DPDK doesn't implement this yet */
391/* Equivalent to PNA is_net_port */
392extern bool
393xdp_p4tc_is_net_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
394
395/* DPDK doesn't implement this yet */
396/* Equivalent to PNA is_host_port */
397extern bool
398xdp_p4tc_is_host_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
399
400
401/* per extern specifics start */
402
403/* in this case it is PNA so we have these helpers like below
404 "is_net_port_skb" but for user specific externs the caller should
405 be doing similar flow:
406 a) populating p4tc_ext_bpf_params struct with their proper
407 parametrization of pipeline id, extern id, and extern specific params
408 b) receiving a response and retrieving their extern-specific data/return
409 codes from res->params
410*/
411
412#define EXTERN_IS_NET_PORT 1234
413#define EXTERN_IS_HOST_PORT 4567
414
415/* Extern control path read (for example, used for register read) */
416extern struct p4tc_ext_bpf_val *
417bpf_p4tc_extern_md_read(struct __sk_buff *skb_ctx,
418 struct p4tc_ext_bpf_params *params,
419 const u32 params__sz) __ksym;
420
421/* Extern control path write (for example, used for register write) */
422extern int bpf_p4tc_extern_md_write(struct __sk_buff *skb_ctx,
423 struct p4tc_ext_bpf_params *params,
424 const u32 params__sz,
425 struct p4tc_ext_bpf_val *val,
426 const u32 val__sz) __ksym;
427
428/* Extern control path read (for example, used for register read) for XDP */
429extern struct p4tc_ext_bpf_val *
430xdp_p4tc_extern_md_read(struct xdp_md *xdp_ctx,
431 struct p4tc_ext_bpf_params *params,
432 const u32 params__sz) __ksym;
433
434/* Extern control path read (for example, used for register write for XDP */
435extern int xdp_p4tc_extern_md_write(struct xdp_md *xdp_ctx,
436 struct p4tc_ext_bpf_params *params,
437 const u32 params__sz,
438 struct p4tc_ext_bpf_val *val,
439 const u32 val__sz) __ksym;
440
441int bpf_p4tc_extern_digest_pack(struct __sk_buff *skb,
442 struct p4tc_ext_bpf_params *params,
443 const u32 params__sz) __ksym;
444
445int xdp_p4tc_extern_digest_pack(struct xdp_md *xdp_ctx,
446 struct p4tc_ext_bpf_params *params,
447 const u32 params__sz) __ksym;
448
449/* Timestamp PNA extern */
450static inline u64 bpf_p4tc_extern_timestamp() {
451 return bpf_ktime_get_ns();
452}
453
454#define U32_MAX ((u32)~0U)
455
456/* Random PNA extern */
457static inline u32 bpf_p4tc_extern_random(u32 min, u32 max) {
458 if (max == U32_MAX)
459 return (min + bpf_get_prandom_u32());
460
461 return (min + bpf_get_prandom_u32()) % (max + 1);
462}
463#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:101
Definition pna.h:114
Definition pna.h:76
Definition pna.h:56
Definition pna.h:66