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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/types.h>
31 #define APP_NAME "icmp_responder_example"
33 #define IF_NAME "libmemif0"
35 #define SOCKET_PATH "/run/vpp/memif.sock"
36 const uint8_t HW_ADDR[6] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
37 const uint8_t IP_ADDR[4] = { 192, 168, 1, 1 };
39 memif_connection_t intf;
42 /* informs user about connected status. private_ctx is used by user to identify
45 on_connect (memif_conn_handle_t conn, void *private_ctx)
47 INFO ("memif connected!");
50 memif_connection_t *c = (memif_connection_t *) private_ctx;
53 alloc_memif_buffers (c);
55 err = memif_refill_queue (conn, 0, -1, c->headroom_size);
56 if (err != MEMIF_ERR_SUCCESS)
58 INFO ("memif_refill_queue: %s", memif_strerror (err));
62 print_memif_details (c);
67 /* informs user about disconnected status. private_ctx is used by user to
68 * identify connection */
70 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
72 INFO ("memif disconnected!");
74 memif_connection_t *c = (memif_connection_t *) private_ctx;
77 free_memif_buffers (c);
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...");
90 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
95 printf ("==============================\n");
97 printf ("==============================\n");
99 "In this example, memif endpoint connects to an external application.\n");
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");
109 printf ("\t-b\tBuffer Size. Default: 2048\n");
110 printf ("\t-h\tHeadroom Size. Default: 0\n");
111 printf ("\t-i\tInterface id. Default: 0\n");
112 printf ("\t-a\tIPv4 address. Default: 192.168.1.1\n");
113 printf ("\t-m\tMac address. Default: aa:aa:aa:aa:aa:aa\n");
114 printf ("\t-?\tShow help and exit.\n");
115 printf ("\t-v\tShow libmemif and memif version information and exit.\n");
119 main (int argc, char *argv[])
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];
129 strncpy (socket_path, SOCKET_PATH, strlen (SOCKET_PATH));
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);
137 while ((opt = getopt (argc, argv, "r:s:b:h:i:a:m:?v")) != -1)
142 if (strncmp (optarg, "master", sizeof (optarg)) == 0)
146 else if (strncmp (optarg, "slave", sizeof (optarg)) == 0)
152 INFO ("Invalid role value: '%s'", optarg);
157 sprintf (socket_path, "%s", optarg);
160 intf.buffer_size = atoi (optarg);
163 intf.headroom_size = atoi (optarg);
169 if (parse_ip4 (optarg, intf.ip_addr) != 0)
171 INFO ("Invalid ipv4 address: %s", optarg);
176 if (parse_mac (optarg, intf.hw_addr) != 0)
178 INFO ("Invalid mac address: %s", optarg);
191 /** Create memif socket
193 * Interfaces are internally stored in a database referenced by memif socket.
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));
199 /* configure autoconnect timer */
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;
208 err = memif_create_socket (&memif_socket, &memif_socket_args, NULL);
209 if (err != MEMIF_ERR_SUCCESS)
211 INFO ("memif_create_socket: %s", memif_strerror (err));
215 /** Create memif interfaces
217 * Both interaces are assigned the same socket and same id to create a
220 if (intf.buffer_size)
221 memif_conn_args.buffer_size = intf.buffer_size;
223 memif_conn_args.socket = memif_socket;
224 memif_conn_args.interface_id = id;
225 strncpy (memif_conn_args.interface_name, IF_NAME,
226 sizeof (memif_conn_args.interface_name));
227 memif_conn_args.is_master = is_master;
230 memif_create (&intf.conn, &memif_conn_args, on_connect, on_disconnect,
231 is_master ? responder : responder_zero_copy, (void *) &intf);
232 if (err != MEMIF_ERR_SUCCESS)
234 INFO ("memif_create_socket: %s", memif_strerror (err));
240 err = memif_poll_event (memif_socket, -1);
242 while (err == MEMIF_ERR_SUCCESS);
249 free_memif_buffers (&intf);
250 memif_delete (&intf.conn);
251 memif_delete_socket (&memif_socket);