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