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