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} __attribute__((preserve_access_index));
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_delete(struct __sk_buff *skb_ctx,
167 const u32 params__sz,
168 void *key, const u32 key__sz) __ksym;
169
170extern int
171xdp_p4tc_entry_delete(struct xdp_md *xdp_ctx,
173 const u32 params__sz,
174 void *key, const u32 key__sz) __ksym;
175
176/* Start generic kfunc interface to any extern */
177
178#define P4TC_EXT_CNT_DIRECT 0x1
179#define P4TC_EXT_CNT_INDIRECT 0x2
180#define P4TC_EXT_METER_DIRECT (1 << 2)
181#define P4TC_EXT_METER_INDIRECT (1 << 3)
182
184 u32 pipe_id;
185 u32 ext_id;
186 u32 inst_id;
187 u32 tbl_id;
188 u32 index;
189 u32 flags;
190 u8 in_params[64]; /* extern specific params if any */
191};
192
194 u32 ext_id;
195 u32 index_id;
196 u32 verdict;
197 u8 out_params[64]; /* specific values if any */
198};
199
200/* Equivalent to PNA indirect counters */
201extern int
202bpf_p4tc_extern_count_pktsnbytes(struct __sk_buff *skb_ctx,
203 struct p4tc_ext_bpf_params *params,
204 const u32 params__sz, void *key, const u32 key__sz) __ksym;
205
206extern int
207bpf_p4tc_extern_count_pkts(struct __sk_buff *skb_ctx,
208 struct p4tc_ext_bpf_params *params,
209 const u32 params__sz, void *key, const u32 key__sz) __ksym;
210
211extern int
212bpf_p4tc_extern_count_bytes(struct __sk_buff *skb_ctx,
213 struct p4tc_ext_bpf_params *params,
214 const u32 params__sz, void *key, const u32 key__sz) __ksym;
215
216extern int
217xdp_p4tc_extern_indirect_count_pktsnbytes(struct xdp_md *xdp_ctx,
218 struct p4tc_ext_bpf_params *params,
219 const u32 params__sz) __ksym;
220
221extern int
222xdp_p4tc_extern_indirect_count_pktsonly(struct xdp_md *xdp_ctx,
223 struct p4tc_ext_bpf_params *params,
224 const u32 params__sz) __ksym;
225
226extern int
227xdp_p4tc_extern_indirect_count_bytesonly(struct xdp_md *xdp_ctx,
228 struct p4tc_ext_bpf_params *params,
229 const u32 params__sz) __ksym;
230
231extern int bpf_p4tc_extern_meter_bytes_color(struct __sk_buff *skb_ctx,
232 struct p4tc_ext_bpf_params *params,
233 const u32 params__sz, void *key,
234 const u32 key__sz) __ksym;
235extern int
236bpf_p4tc_extern_meter_bytes(struct __sk_buff *skb,
237 struct p4tc_ext_bpf_params *params,
238 const u32 params__sz, void *key,
239 const u32 key__sz) __ksym;
240
241extern int bpf_p4tc_extern_meter_pkts_color(struct __sk_buff *skb_ctx,
242 struct p4tc_ext_bpf_params *params,
243 const u32 params__sz, void *key,
244 const u32 key__sz) __ksym;
245extern int
246bpf_p4tc_extern_meter_pkts(struct __sk_buff *skb,
247 struct p4tc_ext_bpf_params *params,
248 const u32 params__sz, void *key,
249 const u32 key__sz) __ksym;
250
251extern int xdp_p4tc_extern_meter_bytes_color(struct xdp_md *xdp_ctx,
252 struct p4tc_ext_bpf_params *params,
253 const u32 params__sz, void *key,
254 const u32 key__sz) __ksym;
255
256extern int xdp_p4tc_extern_meter_bytes(struct xdp_md *xdp_ctx,
257 struct p4tc_ext_bpf_params *params,
258 const u32 params__sz, void *key,
259 const u32 key__sz) __ksym;
260
261extern int xdp_p4tc_extern_meter_pkts_color(struct xdp_md *xdp_ctx,
262 struct p4tc_ext_bpf_params *params,
263 const u32 params__sz, void *key,
264 const u32 key__sz) __ksym;
265
266extern int xdp_p4tc_extern_meter_pkts(struct xdp_md *xdp_ctx,
267 struct p4tc_ext_bpf_params *params,
268 const u32 params__sz, void *key,
269 const u32 key__sz) __ksym;
270
271/* Start checksum related kfuncs */
273 __wsum csum;
274};
275
276/* Equivalent to PNA CRC16 checksum */
277/* Basic checksums are not implemented in DPDK */
278extern u16
279bpf_p4tc_ext_csum_crc16_add(struct p4tc_ext_csum_params *params,
280 const void *data, const u32 data__sz) __ksym;
281
282extern u16
283bpf_p4tc_ext_csum_crc16_get(struct p4tc_ext_csum_params *params) __ksym;
284
285extern void
286bpf_p4tc_ext_csum_crc16_clear(struct p4tc_ext_csum_params *params) __ksym;
287
288/* Equivalent to PNA CRC32 checksum */
289/* Basic checksums are not implemented in DPDK */
290extern u32
291bpf_p4tc_ext_csum_crc32_add(struct p4tc_ext_csum_params *params,
292 const u32 params__sz,
293 const void *data, const u32 data__sz) __ksym;
294
295extern u32
296bpf_p4tc_ext_csum_crc32_get(struct p4tc_ext_csum_params *params,
297 const u32 params__sz) __ksym;
298
299extern void
300bpf_p4tc_ext_csum_crc32_clear(struct p4tc_ext_csum_params *params,
301 const u32 params__sz) __ksym;
302
303extern u16
304bpf_p4tc_ext_csum_16bit_complement_get(struct p4tc_ext_csum_params *params,
305 const u32 params__sz) __ksym;
306
307/* Equivalent to PNA 16bit complement checksum (incremental checksum) */
308extern __wsum
309bpf_p4tc_ext_csum_16bit_complement_add(struct p4tc_ext_csum_params *params,
310 const u32 params__sz,
311 const void *data, int len) __ksym;
312
313extern int
314bpf_p4tc_ext_csum_16bit_complement_sub(struct p4tc_ext_csum_params *params,
315 const u32 params__sz,
316 const void *data, const u32 data__sz) __ksym;
317
318extern void
319bpf_p4tc_ext_csum_16bit_complement_clear(struct p4tc_ext_csum_params *params,
320 const u32 params__sz) __ksym;
321
322extern void
323bpf_p4tc_ext_csum_16bit_complement_set_state(struct p4tc_ext_csum_params *params,
324 const u32 params__sz,
325 u16 csum) __ksym;
326
327/* Equivalent to PNA crc16 hash */
328extern u16
329bpf_p4tc_ext_hash_crc16(const void *data, int len, u16 seed) __ksym;
330
331/* Equivalent to PNA crc16 hash base */
332static inline u16
333bpf_p4tc_ext_hash_base_crc16(const void *data, const u32 data__sz,
334 u32 base, u32 max, u16 seed) {
335 u16 hash = bpf_p4tc_ext_hash_crc16(data, data__sz, seed);
336
337 return (base + (hash % max));
338}
339
340/* Equivalent to PNA crc32 hash */
341extern u32
342bpf_p4tc_ext_hash_crc32(const void *data, const u32 data__sz, u32 seed) __ksym;
343
344/* Equivalent to PNA crc32 hash base */
345static inline u32
346bpf_p4tc_ext_hash_base_crc32(const void *data, const u32 data__sz,
347 u32 base, u32 max, u32 seed) {
348 u32 hash = bpf_p4tc_ext_hash_crc32(data, data__sz, seed);
349
350 return (base + (hash % max));
351}
352
353/* Equivalent to PNA 16-bit ones complement hash */
354extern u16
355bpf_p4tc_ext_hash_16bit_complement(const void *data, const u32 data__sz,
356 u16 seed) __ksym;
357
358/* Equivalent to PNA 16-bit ones complement hash base */
359static inline u16
360bpf_p4tc_ext_hash_base_16bit_complement(const void *data, const u32 data__sz,
361 u32 base, u32 max, u16 seed) {
362 u16 hash = bpf_p4tc_ext_hash_16bit_complement(data, data__sz, seed);
363
364 return (base + (hash % max));
365}
366
367/* DPDK doesn't implement this yet */
368/* Equivalent to PNA is_net_port */
369extern bool
370bpf_p4tc_is_net_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
371
372/* DPDK doesn't implement this yet */
373/* Equivalent to PNA is_host_port */
374extern bool
375bpf_p4tc_is_host_port(struct __sk_buff *skb_ctx, const u32 ifindex) __ksym;
376
377/* DPDK doesn't implement this yet */
378/* Equivalent to PNA is_net_port */
379extern bool
380xdp_p4tc_is_net_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
381
382/* DPDK doesn't implement this yet */
383/* Equivalent to PNA is_host_port */
384extern bool
385xdp_p4tc_is_host_port(struct xdp_md *xdp_ctx, const u32 ifindex) __ksym;
386
387
388/* per extern specifics start */
389
390/* in this case it is PNA so we have these helpers like below
391 "is_net_port_skb" but for user specific externs the caller should
392 be doing similar flow:
393 a) populating p4tc_ext_bpf_params struct with their proper
394 parametrization of pipeline id, extern id, and extern specific params
395 b) receiving a response and retrieving their extern-specific data/return
396 codes from res->params
397*/
398
399#define EXTERN_IS_NET_PORT 1234
400#define EXTERN_IS_HOST_PORT 4567
401
402/* Extern control path read (for example, used for register read) */
403extern struct p4tc_ext_bpf_val *
404bpf_p4tc_extern_md_read(struct __sk_buff *skb_ctx,
405 struct p4tc_ext_bpf_params *params,
406 const u32 params__sz) __ksym;
407
408/* Extern control path write (for example, used for register write) */
409extern int bpf_p4tc_extern_md_write(struct __sk_buff *skb_ctx,
410 struct p4tc_ext_bpf_params *params,
411 const u32 params__sz,
412 struct p4tc_ext_bpf_val *val,
413 const u32 val__sz) __ksym;
414
415/* Extern control path read (for example, used for register read) for XDP */
416extern struct p4tc_ext_bpf_val *
417xdp_p4tc_extern_md_read(struct xdp_md *xdp_ctx,
418 struct p4tc_ext_bpf_params *params,
419 const u32 params__sz) __ksym;
420
421/* Extern control path read (for example, used for register write for XDP */
422extern int xdp_p4tc_extern_md_write(struct xdp_md *xdp_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
428int bpf_p4tc_extern_digest_pack(struct __sk_buff *skb,
429 struct p4tc_ext_bpf_params *params,
430 const u32 params__sz) __ksym;
431
432int xdp_p4tc_extern_digest_pack(struct xdp_md *xdp_ctx,
433 struct p4tc_ext_bpf_params *params,
434 const u32 params__sz) __ksym;
435
436/* Timestamp PNA extern */
437static inline u64 bpf_p4tc_extern_timestamp() {
438 return bpf_ktime_get_ns();
439}
440
441#define U32_MAX ((u32)~0U)
442
443/* Random PNA extern */
444static inline u32 bpf_p4tc_extern_random(u32 min, u32 max) {
445 if (max == U32_MAX)
446 return (min + bpf_get_prandom_u32());
447
448 return (min + bpf_get_prandom_u32()) % (max + 1);
449}
450#endif /* P4C_PNA_H */
Definition ebpf/runtime/psa.h:124
Definition pna.h:183
Definition pna.h:193
Definition pna.h:272
Definition pna.h:101
Definition pna.h:114
Definition pna.h:76
Definition pna.h:56
Definition pna.h:66