32b8ed7579abe7b0363506789af272dcb5ef0367
[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, 0);
56   if (err != MEMIF_ERR_SUCCESS)
57     {
58       INFO ("memif_refill_queue: %s", memif_strerror (err));
59       return err;
60     }
61
62   return 0;
63 }
64
65 /* informs user about disconnected status. private_ctx is used by user to
66  * identify connection */
67 int
68 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
69 {
70   INFO ("memif disconnected!");
71
72   memif_connection_t *c = (memif_connection_t *) private_ctx;
73
74   c->is_connected = 0;
75   free_memif_buffers (c);
76
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...");
81
82   return 0;
83 }
84
85 void
86 print_help ()
87 {
88   printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
89 #ifdef ICMP_DBG
90   printf (" (debug)");
91 #endif
92   printf ("\n");
93   printf ("==============================\n");
94   print_version ();
95   printf ("==============================\n");
96   printf (
97     "In this example, memif endpoint connects to an external application.\n");
98   printf (
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");
112 }
113
114 int
115 main (int argc, char *argv[])
116 {
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];
123   int id = IF_ID;
124
125   strncpy (socket_path, SOCKET_PATH, strlen (SOCKET_PATH));
126
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);
132
133   while ((opt = getopt (argc, argv, "r:s:i:a:h:?v")) != -1)
134     {
135       switch (opt)
136         {
137         case 'r':
138           if (strncmp (optarg, "master", sizeof (optarg)) == 0)
139             {
140               is_master = 1;
141             }
142           else if (strncmp (optarg, "slave", sizeof (optarg)) == 0)
143             {
144               is_master = 0;
145             }
146           else
147             {
148               INFO ("Invalid role value: '%s'", optarg);
149               return -1;
150             }
151           break;
152         case 's':
153           sprintf (socket_path, "%s", optarg);
154           break;
155         case 'i':
156           id = atoi (optarg);
157           break;
158         case 'a':
159           if (parse_ip4 (optarg, intf.ip_addr) != 0)
160             {
161               INFO ("Invalid ipv4 address: %s", optarg);
162               return -1;
163             }
164           break;
165         case 'h':
166           if (parse_mac (optarg, intf.hw_addr) != 0)
167             {
168               INFO ("Invalid mac address: %s", optarg);
169               return -1;
170             }
171           break;
172         case '?':
173           print_help ();
174           return 0;
175         case 'v':
176           print_version ();
177           return 0;
178         }
179     }
180
181   /** Create memif socket
182    *
183    * Interfaces are internally stored in a database referenced by memif socket.
184    */
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));
188
189   /* configure autoconnect timer */
190   if (is_master == 0)
191     {
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;
196     }
197
198   err = memif_create_socket (&memif_socket, &memif_socket_args, NULL);
199   if (err != MEMIF_ERR_SUCCESS)
200     {
201       INFO ("memif_create_socket: %s", memif_strerror (err));
202       goto error;
203     }
204
205   /** Create memif interfaces
206    *
207    * Both interaces are assigned the same socket and same id to create a
208    * loopback.
209    */
210
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;
216
217   err =
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)
221     {
222       INFO ("memif_create_socket: %s", memif_strerror (err));
223       return err;
224     }
225
226   do
227     {
228       err = memif_poll_event (memif_socket, -1);
229     }
230   while (err == MEMIF_ERR_SUCCESS);
231
232   return 0;
233
234 error:
235   ret = -1;
236 done:
237   free_memif_buffers (&intf);
238   memif_delete (&intf.conn);
239   memif_delete_socket (&memif_socket);
240   return ret;
241 }