aboutsummaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/icmp_responder/main.c
blob: b820ad3b12d553df1031fddc5d65798d0f035b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdint.h>
#include <net/if.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <asm/byteorder.h>
#include <byteswap.h>
#include <string.h>
#include <sys/epoll.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

#include <libmemif.h>
#include <icmp_proto.h>

#define APP_NAME "ICMP_Responder"
#define IF_NAME  "memif_connection"


#ifdef ICMP_DBG
#define DBG(...) do {                                               \
                    printf (APP_NAME":%s:%d: ", __func__, __LINE__);         \
                    printf (__VA_ARGS__);                           \
                    printf ("\n");                                  \
                } while (0)
#else
#define DBG(...)
#endif

#define INFO(...) do {                                              \
                    printf ("INFO: "__VA_ARGS__);                   \
                    printf ("\n");                                  \
                } while (0)

/* maximum tx/rx memif buffers */
#define MAX_MEMIF_BUFS 256

typedef struct
{
  uint16_t index;
  /* memif conenction handle */
  memif_conn_handle_t conn;
  /* transmit queue id */
  uint16_t tx_qid;
  /* tx buffers */
  memif_buffer_t *tx_bufs;
  /* allocated tx buffers counter */
  /* number of tx buffers pointing to shared memory */
  uint16_t tx_buf_num;
  /* rx buffers */
  memif_buffer_t *rx_bufs;
  /* allcoated rx buffers counter */
  /* number of rx buffers pointing to shared memory */
  uint16_t rx_buf_num;
  /* interface ip address */
  uint8_t ip_addr[4];
} memif_connection_t;

memif_connection_t memif_connection;
int epfd;

static void
print_memif_details ()
{
  memif_connection_t *c = &memif_connection;
  printf ("MEMIF DETAILS\n");
  printf ("==============================\n");


  memif_details_t md;
  memset (&md, 0, sizeof (md));
  ssize_t buflen = 2048;
  char *buf = malloc (buflen);
  memset (buf, 0, buflen);
  int err, e;

  err = memif_get_details (c->conn, &md, buf, buflen);
  if (err != MEMIF_ERR_SUCCESS)
    {
      INFO ("%s", memif_strerror (err));
      if (err == MEMIF_ERR_NOCONN)
	{
	  free (buf);
	  return;
	}
    }

  printf ("\tinterface name: %s\n", (char *) md.if_name);
  printf ("\tapp name: %s\n", (char *) md.inst_name);
  printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
  printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
  printf ("\tid: %u\n", md.id);
  printf ("\tsecret: %s\n", (char *) md.secret);
  printf ("\trole: ");
  if (md.role)
    printf ("slave\n");
  else
    printf ("master\n");
  printf ("\tmode: ");
  switch (md.mode)
    {
    case 0:
      printf ("ethernet\n");
      break;
    case 1:
      printf ("ip\n");
      break;
    case 2:
      printf ("punt/inject\n");
      break;
    default:
      printf ("unknown\n");
      break;
    }
  printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
  printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
  printf ("\trx queues:\n");
  for (e = 0; e < md.rx_queues_num; e++)
    {
      printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
      printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
      printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
    }
  printf ("\ttx queues:\n");
  for (e = 0; e < md.tx_queues_num; e++)
    {
      printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
      printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
      printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
    }
  printf ("\tlink: ");
  if (md.link_up_down)
    printf ("up\n");
  else
    printf ("down\n");

  free (buf);
}

/* informs user about connected status. private_ctx is used by user to identify connection
    (multiple connections WIP) */
int
on_connect (memif_conn_handle_t conn, void *private_ctx)
{
  INFO ("memif connected!");
  return 0;
}

/* informs user about disconnected status. private_ctx is used by user to identify connection
    (multiple connections WIP) */
int
on_disconnect (memif_conn_handle_t conn, void *private_ctx)
{
  INFO ("memif disconnected!");
  return 0;
}

int
icmpr_memif_delete ()
{
  int err;
  /* disconenct then delete memif connection */
  err = memif_delete (&(&memif_connection)->conn);
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_delete: %s", memif_strerror (err));
  return 0;
}

void
print_help ()
{
  printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
#ifdef ICMP_DBG
  printf (" (debug)");
#endif
  printf ("\n");
  printf ("==============================\n");
  printf ("libmemif version: %s", LIBMEMIF_VERSION);
#ifdef MEMIF_DBG
  printf (" (debug)");
#endif
  printf ("\n");
  printf ("memif version: %d\n", memif_get_version ());
  printf ("\tuse CTRL+C to exit\n");
}

int
icmpr_buffer_alloc (long n, uint16_t qid)
{
  memif_connection_t *c = &memif_connection;
  int err;
  uint16_t r;
  /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 128);
  if (err != MEMIF_ERR_SUCCESS)
    {
      INFO ("memif_buffer_alloc: %s", memif_strerror (err));
      c->tx_buf_num += r;
      return -1;
    }
  c->tx_buf_num += r;
  DBG ("allocated %d/%ld buffers, %u free buffers", r, n,
       MAX_MEMIF_BUFS - c->tx_buf_num);
  return 0;
}

int
icmpr_tx_burst (uint16_t qid)
{
  memif_connection_t *c = &memif_connection;
  int err;
  uint16_t r;
  /* inform peer memif interface about data in shared memory buffers */
  /* mark memif buffers as free */
  err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_tx_burst: %s", memif_strerror (err));
  DBG ("tx: %d/%u", r, c->tx_buf_num);
  c->tx_buf_num -= r;
  return 0;
}

int
icmpr_free ()
{
  /* application cleanup */
  int err;
  memif_connection_t *c = &memif_connection;
  free (c->tx_bufs);
  c->tx_bufs = NULL;
  free (c->rx_bufs);
  c->rx_bufs = NULL;

  err = memif_cleanup ();
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_delete: %s", memif_strerror (err));

  return 0;
}

void
icmpr_exit (int sig)
{
  printf ("\n");
  icmpr_memif_delete ();
  icmpr_free ();
  exit (EXIT_SUCCESS);
}

/* called when event is polled on interrupt file descriptor.
    there are packets in shared memory ready to be received */
int
on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
{
  DBG ("interrupted");
  memif_connection_t *c = &memif_connection;
  int err;
  uint16_t rx;
  /* receive data from shared memory buffers */
  err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
  c->rx_buf_num += rx;

  DBG ("received %d buffers. %u/%u alloc/free buffers",
       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);

  if (icmpr_buffer_alloc (rx, c->tx_qid) < 0)
    {
      INFO ("buffer_alloc error");
      goto error;
    }
  int i;
  for (i = 0; i < rx; i++)
    {
      resolve_packet ((void *) (c->rx_bufs + i)->data,
		      (c->rx_bufs + i)->len,
		      (void *) (c->tx_bufs + i)->data,
		      &(c->tx_bufs + i)->len, c->ip_addr);
    }

  /* mark memif buffers and shared memory buffers as free */
  err = memif_refill_queue (c->conn, qid, rx);
  c->rx_buf_num -= rx;

  DBG ("freed %d buffers. %u/%u alloc/free buffers",
       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);

  icmpr_tx_burst (c->tx_qid);

  return 0;

error:
  err = memif_refill_queue (c->conn, qid, rx);
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_buffer_free: %s", memif_strerror (err));
  c->rx_buf_num -= rx;
  DBG ("freed %d buffers. %u/%u alloc/free buffers",
       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
  return 0;
}

int
icmpr_memif_create (int is_master)
{
  /* setting memif connection arguments */
  memif_conn_args_t args;
  int fd = -1;
  memset (&args, 0, sizeof (args));
  args.is_master = is_master;
  args.log2_ring_size = 10;
  args.buffer_size = 2048;
  args.num_s2m_rings = 2;
  args.num_m2s_rings = 2;
  strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
  args.mode = 0;
  /* socket filename is not specified, because this app is supposed to
     connect to VPP over memif. so default socket filename will be used */
  /* default socketfile = /run/vpp/memif.sock */

  args.interface_id = 0;
  /* last argument for memif_create (void * private_ctx) is used by user
     to identify connection. this context is returned with callbacks */
  int err = memif_create (&(&memif_connection)->conn,
			  &args, on_connect, on_disconnect, on_interrupt,
			  NULL);
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_create: %s", memif_strerror (err));
  return 0;
}

int
main (int argc, char *argv[])
{
  memif_connection_t *c = &memif_connection;

  signal (SIGINT, icmpr_exit);

  /* initialize global memif connection handle */
  c->conn = NULL;
  if (argc == 1)
    c->tx_qid = 0;
  else
    {
      char *end;
      c->tx_qid = strtol (argv[1], &end, 10);
    }
  INFO ("tx qid: %u", c->tx_qid);
  /* alloc memif buffers */
  c->rx_buf_num = 0;
  c->rx_bufs =
    (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
  c->tx_buf_num = 0;
  c->tx_bufs =
    (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
  c->ip_addr[0] = 192;
  c->ip_addr[1] = 168;
  c->ip_addr[2] = 1;
  c->ip_addr[3] = 2;
  /* initialize memory interface */
  int err;
  /* if valid callback is passed as argument, fd event polling will be done by user
     all file descriptors and events will be passed to user in this callback */
  /* if callback is set to NULL libmemif will handle fd event polling */
  err = memif_init (NULL, APP_NAME, NULL, NULL);
  if (err != MEMIF_ERR_SUCCESS)
    INFO ("memif_init: %s", memif_strerror (err));

  print_help ();

  icmpr_memif_create (0);
  print_memif_details ();

  /* main loop */
  while (1)
    {
      if (memif_poll_event (-1) < 0)
	{
	  DBG ("poll_event error!");
	}
    }
}