summaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/common/common.h
blob: 3538e39e23b23906b2408f4e74d5e978d4f3777e (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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2020 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.
 *------------------------------------------------------------------
 */

#ifndef _COMMON_H_
#define _COMMON_H_

#include <libmemif.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#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

struct memif_connection;

typedef int (memif_packet_handler_t) (struct memif_connection *conn);

typedef int (packet_generator_t) (struct memif_connection *c,
				  uint16_t num_pkts);

typedef struct memif_connection
{
  uint16_t index;
  /* memif conenction handle */
  memif_conn_handle_t conn;
  uint8_t is_connected;
  /* 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;
  memif_packet_handler_t *packet_handler;
  /* interface ip address */
  uint8_t ip_addr[4];
  /* interface hw address */
  uint8_t hw_addr[6];
  /* buffer size */
  uint16_t buffer_size;
  /* headroom size */
  uint16_t headroom_size;
} memif_connection_t;

void print_version ();

int parse_ip4 (const char *input, uint8_t out[4]);

int parse_mac (const char *input, uint8_t out[6]);

void alloc_memif_buffers (memif_connection_t *c);

void free_memif_buffers (memif_connection_t *c);

void print_memif_details (memif_connection_t *c);

void print_memif_rx_ring_details (memif_connection_t *c, uint16_t qid);

void print_memif_tx_ring_details (memif_connection_t *c, uint16_t qid);

int send_packets (memif_connection_t *conn, uint16_t qid,
		  packet_generator_t *gen, uint32_t num_pkts,
		  uint16_t max_pkt_size);

/* Expect packets smaller than 2048b */
int responder (memif_conn_handle_t conn, void *private_ctx, uint16_t qid);

/* Expect packets smaller than 2048b */
int responder_zero_copy (memif_conn_handle_t conn, void *private_ctx,
			 uint16_t qid);

/* reply with the same data */
int basic_packet_handler (memif_connection_t *conn);

/* ICMPv4 and ARP handler */
int icmp_packet_handler (memif_connection_t *conn);

#endif /* COMMON_H */