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, 0);
56 if (err != MEMIF_ERR_SUCCESS)
58 INFO ("memif_refill_queue: %s", memif_strerror (err));
65 /* informs user about disconnected status. private_ctx is used by user to
66 * identify connection */
68 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
70 INFO ("memif disconnected!");
72 memif_connection_t *c = (memif_connection_t *) private_ctx;
75 free_memif_buffers (c);
77 /* stop event polling thread */
78 int err = memif_cancel_poll_event (memif_get_socket_handle (conn));
79 if (err != MEMIF_ERR_SUCCESS)
80 INFO ("We are doomed...");
88 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
93 printf ("==============================\n");
95 printf ("==============================\n");
97 "In this example, memif endpoint connects to an external application.\n");
99 "The example application can resolve ARP and reply to ICMPv4 packets.\n");
100 printf ("The program will exit once the interface is disconnected.\n");
101 printf ("==============================\n");
102 printf ("Usage: icmp_responder [OPTIONS]\n\n");
103 printf ("Options:\n");
104 printf ("\t-r\tInterface role <slave|master>. Default: slave\n");
105 printf ("\t-s\tSocket path. Supports abstract socket using @ before the "
106 "path. Default: /run/vpp/memif.sock\n");
107 printf ("\t-i\tInterface id. Default: 0\n");
108 printf ("\t-a\tIPv4 address. Default: 192.168.1.1\n");
109 printf ("\t-h\tMac address. Default: aa:aa:aa:aa:aa:aa\n");
110 printf ("\t-?\tShow help and exit.\n");
111 printf ("\t-v\tShow libmemif and memif version information and exit.\n");
115 main (int argc, char *argv[])
117 memif_socket_args_t memif_socket_args = { 0 };
118 memif_socket_handle_t memif_socket;
119 memif_conn_args_t memif_conn_args = { 0 };
120 int opt, err, ret = 0;
121 uint8_t is_master = 0;
122 char socket_path[108];
125 strncpy (socket_path, SOCKET_PATH, strlen (SOCKET_PATH));
127 /* prepare the private data */
128 memset (&intf, 0, sizeof (intf));
129 intf.packet_handler = icmp_packet_handler;
130 memcpy (intf.ip_addr, IP_ADDR, 4);
131 memcpy (intf.hw_addr, HW_ADDR, 6);
133 while ((opt = getopt (argc, argv, "r:s:i:a:h:?v")) != -1)
138 if (strncmp (optarg, "master", sizeof (optarg)) == 0)
142 else if (strncmp (optarg, "slave", sizeof (optarg)) == 0)
148 INFO ("Invalid role value: '%s'", optarg);
153 sprintf (socket_path, "%s", optarg);
159 if (parse_ip4 (optarg, intf.ip_addr) != 0)
161 INFO ("Invalid ipv4 address: %s", optarg);
166 if (parse_mac (optarg, intf.hw_addr) != 0)
168 INFO ("Invalid mac address: %s", optarg);
181 /** Create memif socket
183 * Interfaces are internally stored in a database referenced by memif socket.
185 sprintf (memif_socket_args.path, "%s", socket_path);
186 /* Set application name */
187 strncpy (memif_socket_args.app_name, APP_NAME, strlen (APP_NAME));
189 /* configure autoconnect timer */
192 memif_socket_args.connection_request_timer.it_value.tv_sec = 2;
193 memif_socket_args.connection_request_timer.it_value.tv_nsec = 0;
194 memif_socket_args.connection_request_timer.it_interval.tv_sec = 2;
195 memif_socket_args.connection_request_timer.it_interval.tv_nsec = 0;
198 err = memif_create_socket (&memif_socket, &memif_socket_args, NULL);
199 if (err != MEMIF_ERR_SUCCESS)
201 INFO ("memif_create_socket: %s", memif_strerror (err));
205 /** Create memif interfaces
207 * Both interaces are assigned the same socket and same id to create a
211 memif_conn_args.socket = memif_socket;
212 memif_conn_args.interface_id = id;
213 strncpy (memif_conn_args.interface_name, IF_NAME,
214 sizeof (memif_conn_args.interface_name));
215 memif_conn_args.is_master = is_master;
218 memif_create (&intf.conn, &memif_conn_args, on_connect, on_disconnect,
219 is_master ? responder : responder_zero_copy, (void *) &intf);
220 if (err != MEMIF_ERR_SUCCESS)
222 INFO ("memif_create_socket: %s", memif_strerror (err));
228 err = memif_poll_event (memif_socket, -1);
230 while (err == MEMIF_ERR_SUCCESS);
237 free_memif_buffers (&intf);
238 memif_delete (&intf.conn);
239 memif_delete_socket (&memif_socket);