blob: d70ecb5647e29a356e73570ba01912e9c51be4e5 [file] [log] [blame]
Jakub Grajciare74c04f2021-01-04 11:28:33 +01001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2020 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#include <stdlib.h>
19#include <sys/types.h>
20#include <inttypes.h>
21#include <string.h>
22#include <stdio.h>
23#include <string.h>
24#include <errno.h>
25#include <unistd.h>
26#include <getopt.h>
27
28#include <libmemif.h>
29#include <common.h>
30
31#define APP_NAME "icmp_responder_example"
32
33#define IF_NAME "libmemif0"
34#define IF_ID 0
35#define SOCKET_PATH "/run/vpp/memif.sock"
36const uint8_t HW_ADDR[6] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
37const uint8_t IP_ADDR[4] = { 192, 168, 1, 1 };
38
39memif_connection_t intf;
40int epfd;
41
42/* informs user about connected status. private_ctx is used by user to identify
43 * connection */
44int
45on_connect (memif_conn_handle_t conn, void *private_ctx)
46{
47 INFO ("memif connected!");
48 int err;
49
50 memif_connection_t *c = (memif_connection_t *) private_ctx;
51
52 c->is_connected = 1;
53 alloc_memif_buffers (c);
54
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +000055 err = memif_refill_queue (conn, 0, -1, c->headroom_size);
Jakub Grajciare74c04f2021-01-04 11:28:33 +010056 if (err != MEMIF_ERR_SUCCESS)
57 {
58 INFO ("memif_refill_queue: %s", memif_strerror (err));
59 return err;
60 }
61
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +000062 print_memif_details (c);
63
Jakub Grajciare74c04f2021-01-04 11:28:33 +010064 return 0;
65}
66
67/* informs user about disconnected status. private_ctx is used by user to
68 * identify connection */
69int
70on_disconnect (memif_conn_handle_t conn, void *private_ctx)
71{
72 INFO ("memif disconnected!");
73
74 memif_connection_t *c = (memif_connection_t *) private_ctx;
75
76 c->is_connected = 0;
77 free_memif_buffers (c);
78
79 /* stop event polling thread */
80 int err = memif_cancel_poll_event (memif_get_socket_handle (conn));
81 if (err != MEMIF_ERR_SUCCESS)
82 INFO ("We are doomed...");
83
84 return 0;
85}
86
87void
88print_help ()
89{
90 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
91#ifdef ICMP_DBG
92 printf (" (debug)");
93#endif
94 printf ("\n");
95 printf ("==============================\n");
96 print_version ();
97 printf ("==============================\n");
98 printf (
99 "In this example, memif endpoint connects to an external application.\n");
100 printf (
101 "The example application can resolve ARP and reply to ICMPv4 packets.\n");
102 printf ("The program will exit once the interface is disconnected.\n");
103 printf ("==============================\n");
104 printf ("Usage: icmp_responder [OPTIONS]\n\n");
105 printf ("Options:\n");
106 printf ("\t-r\tInterface role <slave|master>. Default: slave\n");
107 printf ("\t-s\tSocket path. Supports abstract socket using @ before the "
108 "path. Default: /run/vpp/memif.sock\n");
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000109 printf ("\t-b\tBuffer Size. Default: 2048\n");
110 printf ("\t-h\tHeadroom Size. Default: 0\n");
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100111 printf ("\t-i\tInterface id. Default: 0\n");
112 printf ("\t-a\tIPv4 address. Default: 192.168.1.1\n");
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000113 printf ("\t-m\tMac address. Default: aa:aa:aa:aa:aa:aa\n");
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100114 printf ("\t-?\tShow help and exit.\n");
115 printf ("\t-v\tShow libmemif and memif version information and exit.\n");
116}
117
118int
119main (int argc, char *argv[])
120{
121 memif_socket_args_t memif_socket_args = { 0 };
122 memif_socket_handle_t memif_socket;
123 memif_conn_args_t memif_conn_args = { 0 };
124 int opt, err, ret = 0;
125 uint8_t is_master = 0;
126 char socket_path[108];
127 int id = IF_ID;
128
Tianyu Lid7c96322023-07-12 05:51:42 +0000129 strncpy (socket_path, SOCKET_PATH, sizeof (SOCKET_PATH));
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100130
131 /* prepare the private data */
132 memset (&intf, 0, sizeof (intf));
133 intf.packet_handler = icmp_packet_handler;
134 memcpy (intf.ip_addr, IP_ADDR, 4);
135 memcpy (intf.hw_addr, HW_ADDR, 6);
136
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000137 while ((opt = getopt (argc, argv, "r:s:b:h:i:a:m:?v")) != -1)
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100138 {
139 switch (opt)
140 {
141 case 'r':
142 if (strncmp (optarg, "master", sizeof (optarg)) == 0)
143 {
144 is_master = 1;
145 }
146 else if (strncmp (optarg, "slave", sizeof (optarg)) == 0)
147 {
148 is_master = 0;
149 }
150 else
151 {
152 INFO ("Invalid role value: '%s'", optarg);
153 return -1;
154 }
155 break;
156 case 's':
157 sprintf (socket_path, "%s", optarg);
158 break;
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000159 case 'b':
160 intf.buffer_size = atoi (optarg);
161 break;
162 case 'h':
163 intf.headroom_size = atoi (optarg);
164 break;
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100165 case 'i':
166 id = atoi (optarg);
167 break;
168 case 'a':
169 if (parse_ip4 (optarg, intf.ip_addr) != 0)
170 {
171 INFO ("Invalid ipv4 address: %s", optarg);
172 return -1;
173 }
174 break;
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000175 case 'm':
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100176 if (parse_mac (optarg, intf.hw_addr) != 0)
177 {
178 INFO ("Invalid mac address: %s", optarg);
179 return -1;
180 }
181 break;
182 case '?':
183 print_help ();
184 return 0;
185 case 'v':
186 print_version ();
187 return 0;
188 }
189 }
190
191 /** Create memif socket
192 *
193 * Interfaces are internally stored in a database referenced by memif socket.
194 */
195 sprintf (memif_socket_args.path, "%s", socket_path);
196 /* Set application name */
197 strncpy (memif_socket_args.app_name, APP_NAME, strlen (APP_NAME));
198
199 /* configure autoconnect timer */
200 if (is_master == 0)
201 {
202 memif_socket_args.connection_request_timer.it_value.tv_sec = 2;
203 memif_socket_args.connection_request_timer.it_value.tv_nsec = 0;
204 memif_socket_args.connection_request_timer.it_interval.tv_sec = 2;
205 memif_socket_args.connection_request_timer.it_interval.tv_nsec = 0;
206 }
207
208 err = memif_create_socket (&memif_socket, &memif_socket_args, NULL);
209 if (err != MEMIF_ERR_SUCCESS)
210 {
211 INFO ("memif_create_socket: %s", memif_strerror (err));
212 goto error;
213 }
214
215 /** Create memif interfaces
216 *
217 * Both interaces are assigned the same socket and same id to create a
218 * loopback.
219 */
Mohsin Kazmi6e284ef2022-07-27 15:04:01 +0000220 if (intf.buffer_size)
221 memif_conn_args.buffer_size = intf.buffer_size;
Tianyu Lid7c96322023-07-12 05:51:42 +0000222 else
223 intf.buffer_size = 2048;
Jakub Grajciare74c04f2021-01-04 11:28:33 +0100224
225 memif_conn_args.socket = memif_socket;
226 memif_conn_args.interface_id = id;
227 strncpy (memif_conn_args.interface_name, IF_NAME,
228 sizeof (memif_conn_args.interface_name));
229 memif_conn_args.is_master = is_master;
230
231 err =
232 memif_create (&intf.conn, &memif_conn_args, on_connect, on_disconnect,
233 is_master ? responder : responder_zero_copy, (void *) &intf);
234 if (err != MEMIF_ERR_SUCCESS)
235 {
236 INFO ("memif_create_socket: %s", memif_strerror (err));
237 return err;
238 }
239
240 do
241 {
242 err = memif_poll_event (memif_socket, -1);
243 }
244 while (err == MEMIF_ERR_SUCCESS);
245
246 return 0;
247
248error:
249 ret = -1;
250done:
251 free_memif_buffers (&intf);
252 memif_delete (&intf.conn);
253 memif_delete_socket (&memif_socket);
254 return ret;
255}