-
Notifications
You must be signed in to change notification settings - Fork 136
/
forward.c
204 lines (177 loc) · 7.17 KB
/
forward.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*********************************************************************
* openNetVM
* https://sdnfv.github.io
*
* BSD LICENSE
*
* Copyright(c)
* 2015-2019 George Washington University
* 2015-2019 University of California Riverside
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* forward.c - an example using onvm. Forwards packets to a DST NF.
********************************************************************/
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <unistd.h>
#include <rte_common.h>
#include <rte_ip.h>
#include <rte_mbuf.h>
#include "onvm_nflib.h"
#include "onvm_pkt_helper.h"
#define NF_TAG "simple_forward"
/* number of package between each print */
static uint32_t print_delay = 1000000;
static uint32_t destination;
/*
* Print a usage message
*/
static void
usage(const char *progname) {
printf("Usage:\n");
printf("%s [EAL args] -- [NF_LIB args] -- -d <destination> -p <print_delay>\n", progname);
printf("%s -F <CONFIG_FILE.json> [EAL args] -- [NF_LIB args] -- [NF args]\n\n", progname);
printf("Flags:\n");
printf(" - `-d <dst>`: destination service ID to foward to\n");
printf(" - `-p <print_delay>`: number of packets between each print, e.g. `-p 1` prints every packets.\n");
}
/*
* Parse the application arguments.
*/
static int
parse_app_args(int argc, char *argv[], const char *progname) {
int c, dst_flag = 0;
while ((c = getopt(argc, argv, "d:p:")) != -1) {
switch (c) {
case 'd':
destination = strtoul(optarg, NULL, 10);
dst_flag = 1;
break;
case 'p':
print_delay = strtoul(optarg, NULL, 10);
break;
case '?':
usage(progname);
if (optopt == 'd')
RTE_LOG(INFO, APP, "Option -%c requires an argument.\n", optopt);
else if (optopt == 'p')
RTE_LOG(INFO, APP, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
RTE_LOG(INFO, APP, "Unknown option `-%c'.\n", optopt);
else
RTE_LOG(INFO, APP, "Unknown option character `\\x%x'.\n", optopt);
return -1;
default:
usage(progname);
return -1;
}
}
if (!dst_flag) {
RTE_LOG(INFO, APP, "Simple Forward NF requires destination flag -d.\n");
return -1;
}
return optind;
}
/*
* This function displays stats. It uses ANSI terminal codes to clear
* screen when called. It is called from a single non-master
* thread in the server process, when the process is run with more
* than one lcore enabled.
*/
static void
do_stats_display(struct rte_mbuf *pkt) {
const char clr[] = {27, '[', '2', 'J', '\0'};
const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
static uint64_t pkt_process = 0;
struct rte_ipv4_hdr *ip;
pkt_process += print_delay;
/* Clear screen and move to top left */
printf("%s%s", clr, topLeft);
printf("PACKETS\n");
printf("-----\n");
printf("Port : %d\n", pkt->port);
printf("Size : %d\n", pkt->pkt_len);
printf("N° : %" PRIu64 "\n", pkt_process);
printf("\n\n");
ip = onvm_pkt_ipv4_hdr(pkt);
if (ip != NULL) {
onvm_pkt_print(pkt);
} else {
printf("No IP4 header found\n");
}
}
static int
packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta,
__attribute__((unused)) struct onvm_nf_local_ctx *nf_local_ctx) {
static uint32_t counter = 0;
if (++counter == print_delay) {
do_stats_display(pkt);
counter = 0;
}
meta->action = ONVM_NF_ACTION_TONF;
meta->destination = destination;
return 0;
}
int
main(int argc, char *argv[]) {
struct onvm_nf_local_ctx *nf_local_ctx;
struct onvm_nf_function_table *nf_function_table;
int arg_offset;
const char *progname = argv[0];
nf_local_ctx = onvm_nflib_init_nf_local_ctx();
onvm_nflib_start_signal_handler(nf_local_ctx, NULL);
nf_function_table = onvm_nflib_init_nf_function_table();
nf_function_table->pkt_handler = &packet_handler;
if ((arg_offset = onvm_nflib_init(argc, argv, NF_TAG, nf_local_ctx, nf_function_table)) < 0) {
onvm_nflib_stop(nf_local_ctx);
if (arg_offset == ONVM_SIGNAL_TERMINATION) {
printf("Exiting due to user termination\n");
return 0;
} else {
rte_exit(EXIT_FAILURE, "Failed ONVM init\n");
}
}
argc -= arg_offset;
argv += arg_offset;
if (parse_app_args(argc, argv, progname) < 0) {
onvm_nflib_stop(nf_local_ctx);
rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
}
onvm_nflib_run(nf_local_ctx);
onvm_nflib_stop(nf_local_ctx);
printf("If we reach here, program is ending\n");
return 0;
}