P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpf_test.h
1/*
2 * Copyright 2020 Orange
3 * SPDX-FileCopyrightText: 2020 Orange
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef P4C_UBPF_TEST_H
9#define P4C_UBPF_TEST_H
10
11#include <stdarg.h>
12#include <stdio.h>
13#include <stdint.h>
14
15#define MAX_PRINTF_LENGTH 80
16
17/* simple descriptor which replaces the DPDK dp_packet structure */
18struct dp_packet {
19 void *data; /* First byte actually in use. */
20 uint32_t size_; /* Number of bytes in use. */
21};
22
23static inline void ubpf_printf_test(const char *fmt, ...) {
24 va_list args;
25 va_start(args, fmt);
26 char str[MAX_PRINTF_LENGTH];
27 if (vsnprintf(str, MAX_PRINTF_LENGTH, fmt, args) >= 0)
28 printf("%s\n", str);
29 va_end(args);
30}
31
32static inline void *ubpf_packet_data_test(void *ctx) {
33 struct dp_packet *dp = (struct dp_packet *) ctx;
34 return dp->data;
35}
36
37static inline void *ubpf_adjust_head_test(void *ctx, int offset) {
38 struct dp_packet *dp = (struct dp_packet *) ctx;
39
40 if (offset == 0) {
41 return dp->data;
42 } else if (offset > 0) {
43 dp->data = realloc(dp->data, dp->size_ + offset);
44 memcpy((char *) dp->data + offset, dp->data, dp->size_);
45 dp->size_ += offset;
46 return dp->data;
47 } else {
48 int ofs = abs(offset);
49 memcpy(dp->data, (char *) dp->data + ofs, dp->size_ - ofs);
50 dp->data = realloc(dp->data, dp->size_ - ofs);
51 dp->size_ -= ofs;
52 return dp->data;
53 }
54}
55
56static inline uint32_t ubpf_truncate_packet_test(void *ctx, int maxlen)
57{
58 struct dp_packet *dp = (struct dp_packet *) ctx;
59 uint32_t cutlen= 0;
60
61 if (maxlen < 0)
62 return 0;
63
64 if (maxlen >= dp->size_) {
65 cutlen = 0;
66 } else {
67 cutlen = dp->size_ - maxlen;
68 dp->data = realloc(dp->data, maxlen);
69 dp->size_ = maxlen;
70 }
71
72 return cutlen;
73}
74
75
76#endif //P4C_UBPF_TEST_H
Definition ubpf_test.h:18