libmemif: add support for custom buffer-size and headroom in icmp example app
[vpp.git] / extras / libmemif / examples / icmp_responder / main.c
1 /*
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"
36 const uint8_t HW_ADDR[6] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
37 const uint8_t IP_ADDR[4] = { 192, 168, 1, 1 };
38
39 memif_connection_t intf;
40 int epfd;
41
42 /* informs user about connected status. private_ctx is used by user to identify
43  * connection */
44 int
45 on_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
55   err = memif_refill_queue (conn, 0, -1, c->headroom_size);
56   if (err != MEMIF_ERR_SUCCESS)
57     {
58       INFO ("memif_refill_queue: %s", memif_strerror (err));
59       return err;
60     }
61
62   print_memif_details (c);
63
64   return 0;
65 }
66
67 /* informs user about disconnected status. private_ctx is used by user to
68  * identify connection */
69 int
70 on_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
87 void
88 print_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");
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");
116 }
117
118 int
119 main (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
129   strncpy (socket_path, SOCKET_PATH, strlen (SOCKET_PATH));
130
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
137   while ((opt = getopt (argc, argv, "r:s:b:h:i:a:m:?v")) != -1)
138     {
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;
159         case 'b':
160           intf.buffer_size = atoi (optarg);
161           break;
162         case 'h':
163           intf.headroom_size = atoi (optarg);
164           break;
165         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;
175         case 'm':
176           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    */
220   if (intf.buffer_size)
221     memif_conn_args.buffer_size = intf.buffer_size;
222
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;
228
229   err =
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)
233     {
234       INFO ("memif_create_socket: %s", memif_strerror (err));
235       return err;
236     }
237
238   do
239     {
240       err = memif_poll_event (memif_socket, -1);
241     }
242   while (err == MEMIF_ERR_SUCCESS);
243
244   return 0;
245
246 error:
247   ret = -1;
248 done:
249   free_memif_buffers (&intf);
250   memif_delete (&intf.conn);
251   memif_delete_socket (&memif_socket);
252   return ret;
253 }