API Reference Manual  1.46.0
ipsec_crypto/odp_ipsec_fwd_db.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2014-2018 Linaro Limited
3  */
4 
7 /* enable strtok */
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <odp_api.h>
16 #include <odp/helper/odph_api.h>
17 
18 #include <odp_ipsec_fwd_db.h>
19 
21 fwd_db_t *fwd_db;
22 
23 void init_fwd_db(void)
24 {
25  odp_shm_t shm;
26 
27  shm = odp_shm_reserve("shm_fwd_db",
28  sizeof(fwd_db_t),
29  ODP_CACHE_LINE_SIZE,
30  0);
31 
32  if (shm == ODP_SHM_INVALID) {
33  ODPH_ERR("Error: shared mem reserve failed.\n");
34  exit(EXIT_FAILURE);
35  }
36 
37  fwd_db = odp_shm_addr(shm);
38 
39  if (fwd_db == NULL) {
40  ODPH_ERR("Error: shared mem alloc failed.\n");
41  exit(EXIT_FAILURE);
42  }
43  memset(fwd_db, 0, sizeof(*fwd_db));
44 }
45 
46 int create_fwd_db_entry(char *input, char **if_names, int if_count)
47 {
48  int pos = 0, i, match = 0;
49  char *local;
50  char *str;
51  char *save;
52  char *token;
53  fwd_db_entry_t *entry = &fwd_db->array[fwd_db->index];
54 
55  /* Verify we haven't run out of space */
56  if (MAX_DB <= fwd_db->index)
57  return -1;
58 
59  /* Make a local copy */
60  local = malloc(strlen(input) + 1);
61  if (NULL == local)
62  return -1;
63  strcpy(local, input);
64 
65  /* Setup for using "strtok_r" to search input string */
66  str = local;
67  save = NULL;
68 
69  /* Parse tokens separated by ',' */
70  while (NULL != (token = strtok_r(str, ",", &save))) {
71  str = NULL; /* reset str for subsequent strtok_r calls */
72 
73  /* Parse token based on its position */
74  switch (pos) {
75  case 0:
76  parse_ipv4_string(token,
77  &entry->subnet.addr,
78  &entry->subnet.mask);
79  break;
80  case 1:
81  odph_strcpy(entry->oif, token, OIF_LEN);
82  for (i = 0; i < if_count; i++) {
83  if (!strcmp(if_names[i], entry->oif)) {
84  match = 1;
85  break;
86  }
87  }
88  if (!match) {
89  printf("ERROR: interface name not correct for route\n");
90  free(local);
91  return -1;
92  }
93  break;
94  case 2:
95  parse_mac_string(token, entry->dst_mac);
96  break;
97  default:
98  printf("ERROR: extra token \"%s\" at position %d\n",
99  token, pos);
100  break;
101  }
102 
103  /* Advance to next position */
104  pos++;
105  }
106 
107  /* Verify we parsed exactly the number of tokens we expected */
108  if (3 != pos) {
109  printf("ERROR: \"%s\" contains %d tokens, expected 3\n",
110  input,
111  pos);
112  free(local);
113  return -1;
114  }
115 
116  /* Add route to the list */
117  fwd_db->index++;
118  entry->next = fwd_db->list;
119  fwd_db->list = entry;
120 
121  free(local);
122  return 0;
123 }
124 
125 void resolve_fwd_db(char *intf, odp_pktio_t pktio, odp_pktout_queue_t pktout,
126  uint8_t *mac)
127 {
128  fwd_db_entry_t *entry;
129 
130  /* Walk the list and attempt to set output queue and MAC */
131  for (entry = fwd_db->list; NULL != entry; entry = entry->next) {
132  if (strcmp(intf, entry->oif))
133  continue;
134 
135  entry->pktio = pktio;
136  entry->pktout = pktout;
137  memcpy(entry->src_mac, mac, ODPH_ETHADDR_LEN);
138  }
139 }
140 
141 void dump_fwd_db_entry(fwd_db_entry_t *entry)
142 {
143  char subnet_str[MAX_STRING];
144  char mac_str[MAX_STRING];
145 
146  printf(" %s %s %s\n",
147  ipv4_subnet_str(subnet_str, &entry->subnet),
148  entry->oif,
149  mac_addr_str(mac_str, entry->dst_mac));
150 }
151 
152 void dump_fwd_db(void)
153 {
154  fwd_db_entry_t *entry;
155 
156  printf("\n"
157  "Routing table\n"
158  "-------------\n");
159 
160  for (entry = fwd_db->list; NULL != entry; entry = entry->next)
161  dump_fwd_db_entry(entry);
162 }
163 
164 fwd_db_entry_t *find_fwd_db_entry(uint32_t dst_ip)
165 {
166  fwd_db_entry_t *entry;
167 
168  for (entry = fwd_db->list; NULL != entry; entry = entry->next)
169  if (entry->subnet.addr == (dst_ip & entry->subnet.mask))
170  break;
171  return entry;
172 }
#define ODP_SHM_INVALID
Invalid shared memory block.
void * odp_shm_addr(odp_shm_t shm)
Shared memory block address.
odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, uint32_t flags)
Reserve a contiguous block of shared memory.
The OpenDataPlane API.