aboutsummaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/common/common.c
blob: 5af42eaf63be6bc76d112d675baf05a19c908ccc (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
#include <common.h>

void
print_memif_ring_details (memif_connection_t *c, uint16_t qid, uint8_t is_rx)
{
  /* TODO: print memif shared memory details */
}

void
print_memif_rx_ring_details (memif_connection_t *c, uint16_t qid)
{
  print_memif_ring_details (c, qid, /* RX */ 1);
}

void
print_memif_tx_ring_details (memif_connection_t *c, uint16_t qid)
{
  print_memif_ring_details (c, qid, /* TX */ 0);
}

void
print_version ()
{
  printf ("libmemif version: %s, memif version: %s\n", LIBMEMIF_VERSION,
	  memif_get_version_str ());
}

int
parse_ip4 (const char *input, uint8_t out[4])
{
  char *ui, *end;
  char *tmp = strdup (input);

  ui = strtok (tmp, ".");
  if (ui == NULL)
    return -1;
  out[0] = strtol (ui, &end, 10);

  ui = strtok (NULL, ".");
  if (ui == NULL)
    return -1;
  out[1] = strtol (ui, &end, 10);

  ui = strtok (NULL, ".");
  if (ui == NULL)
    return -1;
  out[2] = strtol (ui, &end, 10);

  ui = strtok (NULL, ".");
  if (ui == NULL)
    return -1;
  out[3] = strtol (ui, &end, 10);

  free (tmp);

  return 0;
}

int
parse_mac (const char *input, uint8_t out[6])
{
  char *ui, *end;
  char *tmp = strdup (input);

  ui = strtok (tmp, ":");
  if (ui == NULL)
    return -1;
  out[0] = strtol (ui, &end, 16);
  ui = strtok (NULL, ":");
  if (ui == NULL)
    return -1;
  out[1] = strtol (ui, &end, 16);
  ui = strtok (NULL, ":");
  if (ui == NULL)
    return -1;
  out[2] = strtol (ui, &end, 16);
  ui = strtok (NULL, ":");
  if (ui == NULL)
    return -1;
  out[3] = strtol (ui, &end, 16);
  ui = strtok (NULL, ":");
  if (ui == NULL)
    return -1;
  out[4] = strtol (ui, &end, 16);
  ui = strtok (NULL, ":");
  if (ui == NULL)
    return -1;
  out[5] = strtol (ui, &end, 16);

  free (tmp);

  return 0;
}

void
alloc_memif_buffers (memif_connection_t *c)
{
  c->rx_bufs =
    (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
  c->rx_buf_num = 0;
  c->tx_bufs =
    (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
  c->tx_buf_num = 0;
}

void
free_memif_buffers (memif_connection_t *c)
{
  if (c->rx_bufs != NULL)
    free (c->rx_bufs);
  c->rx_bufs = NULL;
  c->rx_buf_num = 0;
  if (c->tx_bufs != NULL)
    free (c->tx_bufs);
  c->tx_bufs = NULL;
  c->tx_buf_num = 0;
}

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

  memif_details_t md;
  memset (&md, 0, sizeof (md));
  ssize_t buflen = 2048;
  char *buf = (char *) 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 path: %s\n", (char *) md.socket_path);
  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);
}