aboutsummaryrefslogtreecommitdiff
path: root/example/ipsec_offload/odp_ipsec_offload_fwd_db.c
blob: f707ab9aa6b33cfdb8a289622c4d23ac213730d1 (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
/* Copyright (c) 2017-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/* enable strtok */
#define _POSIX_C_SOURCE 200112L

#include <stdlib.h>
#include <string.h>

#include <example_debug.h>
#include <odp.h>

#include <odp_ipsec_offload_fwd_db.h>

/**
 * Pointer to Flow cache table
 */
flow_bucket_t *flow_table;

/**
 * bucket count. It will be updated with user argument if provided
 */
uint32_t bucket_count = DEFAULT_BUCKET_COUNT;

/** Global pointer to fwd db */
fwd_db_t *fwd_db;

void init_routing_table(void)
{
	odp_shm_t		hash_shm;
	uint32_t		i;
	flow_bucket_t		*bucket;

	/*Reserve memory for Routing hash table*/
	hash_shm = odp_shm_reserve("route_table",
				   sizeof(flow_bucket_t) * bucket_count,
				   ODP_CACHE_LINE_SIZE, 0);
	if (hash_shm == ODP_SHM_INVALID)
		EXAMPLE_ABORT("Error: shared mem alloc failed.\n");
	flow_table = odp_shm_addr(hash_shm);
	if (!flow_table)
		EXAMPLE_ABORT("Error: shared mem alloc failed.\n");
	/*Inialize Locks*/
	for (i = 0; i < bucket_count; i++) {
		bucket = &flow_table[i];
		LOCK_INIT(&bucket->lock);
	}

	memset(flow_table, 0, bucket_count * sizeof(flow_bucket_t));
}

void init_fwd_db(void)
{
	odp_shm_t shm;

	shm = odp_shm_reserve("shm_fwd_db",
			      sizeof(fwd_db_t),
			      ODP_CACHE_LINE_SIZE,
			      0);

	fwd_db = odp_shm_addr(shm);

	if (fwd_db == NULL)
		EXAMPLE_ABORT("Error: shared mem alloc failed.\n");
	memset(fwd_db, 0, sizeof(*fwd_db));
}

int create_fwd_db_entry(char *input, char **if_names, int if_count, int entries)
{
	int pos = 0, i, match = 0, count = 0;
	char *local;
	char *str;
	char *save;
	char *token;
	fwd_db_entry_t *entry = &fwd_db->array[fwd_db->index];

	/* Verify we haven't run out of space */
	if (MAX_DB <= fwd_db->index)
		return -1;

	/* Make a local copy */
	local = malloc(strlen(input) + 1);
	if (NULL == local)
		return -1;
	strcpy(local, input);

	/* Setup for using "strtok_r" to search input string */
	str = local;
	save = NULL;

	/* Parse tokens separated by ':' */
	while (NULL != (token = strtok_r(str, ":", &save))) {
		str = NULL;  /* reset str for subsequent strtok_r calls */

		/* Parse token based on its position */
		switch (pos) {
		case 0:
			parse_ipv4_string(token,
					  &entry->subnet.addr,
					  &entry->subnet.mask);
			break;
		case 1:
			strncpy(entry->oif, token, OIF_LEN - 1);
			entry->oif[OIF_LEN - 1] = 0;
			for (i = 0; i < if_count; i++) {
				if (!strcmp(if_names[i], entry->oif)) {
					match = 1;
					break;
				}
			}
			if (!match) {
				printf("ERROR: interface name not correct for route\n");
				free(local);
				return -1;
			}
			break;
		case 2:
			parse_mac_string(token, entry->dst_mac);
			break;
		default:
			printf("ERROR: extra token \"%s\" at position %d\n",
			       token, pos);
			break;
		}

		/* Advance to next position */
		pos++;
	}

	/* Verify we parsed exactly the number of tokens we expected */
	if (3 != pos) {
		printf("ERROR: \"%s\" contains %d tokens, expected 3\n",
		       input,
		       pos);
		free(local);
		return -1;
	}

	/* Add route to the list */
	fwd_db->index++;
	entry->next = fwd_db->list;
	fwd_db->list = entry;

	count++;

	while (count < entries) {
		fwd_db_entry_t *new_entry = &fwd_db->array[fwd_db->index];

		/* Verify we haven't run out of space */
		if (MAX_DB <= fwd_db->index)
			return -1;

		new_entry->subnet.addr = entry->subnet.addr + count;
		new_entry->subnet.mask = entry->subnet.mask;
		strncpy(new_entry->oif, entry->oif, OIF_LEN - 1);
		new_entry->oif[OIF_LEN - 1] = 0;
		new_entry->dst_mac[0] = entry->dst_mac[0];
		new_entry->dst_mac[1] = entry->dst_mac[1];
		new_entry->dst_mac[2] = entry->dst_mac[2];
		new_entry->dst_mac[3] = entry->dst_mac[3];
		new_entry->dst_mac[4] = entry->dst_mac[4];
		new_entry->dst_mac[5] = entry->dst_mac[5];

		/* Add route to the list */
		fwd_db->index++;
		new_entry->next = fwd_db->list;
		fwd_db->list = new_entry;
		count++;
	}

	free(local);
	return 0;
}

void resolve_fwd_db(char *intf, odp_pktout_queue_t pktout, uint8_t *mac)
{
	fwd_db_entry_t *entry;

	/* Walk the list and attempt to set output queue and MAC */
	for (entry = fwd_db->list; NULL != entry; entry = entry->next) {
		if (strcmp(intf, entry->oif))
			continue;

		entry->pktout = pktout;
		memcpy(entry->src_mac, mac, ODPH_ETHADDR_LEN);
	}
}

void dump_fwd_db_entry(fwd_db_entry_t *entry)
{
	char subnet_str[MAX_STRING];
	char mac_str[MAX_STRING];

	printf(" %s %s %s\n",
	       ipv4_subnet_str(subnet_str, &entry->subnet),
	       entry->oif,
	       mac_addr_str(mac_str, entry->dst_mac));
}

void dump_fwd_db(void)
{
	fwd_db_entry_t *entry;

	printf("\n"
	       "Routing table\n"
	       "-------------\n");

	for (entry = fwd_db->list; NULL != entry; entry = entry->next)
		dump_fwd_db_entry(entry);
}

fwd_db_entry_t *find_fwd_db_entry(uint32_t dst_ip)
{
	fwd_db_entry_t *entry;

	for (entry = fwd_db->list; NULL != entry; entry = entry->next)
		if (entry->subnet.addr == (dst_ip & entry->subnet.mask))
			break;
	return entry;
}