summaryrefslogtreecommitdiff
path: root/src/vf/spp_forward.c
blob: 1ada73e1aaa243f909fe3cf504a23b4c16f8b885 (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
/*
 *   BSD LICENSE
 *
 *   Copyright(c) 2017 Nippon Telegraph and Telephone Corporation
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Nippon Telegraph and Telephone Corporation
 *       nor the names of its contributors may be used to endorse or
 *       promote products derived from this software without specific
 *       prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <rte_cycles.h>

#include "spp_vf.h"
#include "ringlatencystats.h"
#include "spp_forward.h"

#define RTE_LOGTYPE_FORWARD RTE_LOGTYPE_USER1

/* A set of port info of rx and tx */
struct forward_rxtx {
	struct spp_port_info rx; /* rx port */
	struct spp_port_info tx; /* tx port */
};

/* Information on the path used for forward. */
struct forward_path {
	char name[SPP_NAME_STR_LEN];    /* component name          */
	volatile enum spp_component_type type;
					/* component type          */
	int num;                        /* number of receive ports */
	struct forward_rxtx ports[RTE_MAX_ETHPORTS];
					/* port used for transfer  */
};

/* Information for forward. */
struct forward_info {
	volatile int ref_index; /* index to reference area */
	volatile int upd_index; /* index to update area    */
	struct forward_path path[SPP_INFO_AREA_MAX];
				/* Information of data path */
};

struct forward_info g_forward_info[RTE_MAX_LCORE];

/* Clear info */
void
spp_forward_init(void)
{
	int cnt = 0;
	memset(&g_forward_info, 0x00, sizeof(g_forward_info));
	for (cnt = 0; cnt < RTE_MAX_LCORE; cnt++) {
		g_forward_info[cnt].ref_index = 0;
		g_forward_info[cnt].upd_index = 1;
	}
}

/* Clear info for one element. */
static void
clear_forward_info(int id)
{
	memset(&g_forward_info[id].path, 0x00, sizeof(struct forward_path));
}

/* Update forward info */
int
spp_forward_update(struct spp_component_info *component)
{
	int cnt = 0;
	int num_rx = component->num_rx_port;
	int num_tx = component->num_tx_port;
	int max = (num_rx > num_tx)?num_rx*num_tx:num_tx;
	struct forward_info *info = &g_forward_info[component->component_id];
	struct forward_path *path = &info->path[info->upd_index];

	/* Forward component allows only one receiving port. */
	if ((component->type == SPP_COMPONENT_FORWARD) &&
			unlikely(num_rx > 1)) {
		RTE_LOG(ERR, FORWARD,
			"Component[%d] Setting error. (type = %d, rx = %d)\n",
			component->component_id, component->type, num_rx);
		return -1;
	}

	/* Component allows only one transmit port. */
	if (unlikely(num_tx != 0) && unlikely(num_tx != 1)) {
		RTE_LOG(ERR, FORWARD,
			"Component[%d] Setting error. (type = %d, tx = %d)\n",
			component->component_id, component->type, num_tx);
		return -1;
	}

	clear_forward_info(component->component_id);

	RTE_LOG(INFO, FORWARD,
			"Component[%d] Start update component. (name = %s, type = %d)\n",
			component->component_id,
			component->name,
			component->type);

	memcpy(&path->name, component->name, SPP_NAME_STR_LEN);
	path->type = component->type;
	path->num = component->num_rx_port;
	for (cnt = 0; cnt < num_rx; cnt++)
		memcpy(&path->ports[cnt].rx, component->rx_ports[cnt],
				sizeof(struct spp_port_info));

	/* Transmit port is set according with larger num_rx / num_tx. */
	for (cnt = 0; cnt < max; cnt++)
		memcpy(&path->ports[cnt].tx, component->tx_ports[0],
				sizeof(struct spp_port_info));

	info->upd_index = info->ref_index;
	while (likely(info->ref_index == info->upd_index))
		rte_delay_us_block(SPP_CHANGE_UPDATE_INTERVAL);

	RTE_LOG(INFO, FORWARD,
			"Component[%d] Complete update component. (name = %s, type = %d)\n",
			component->component_id,
			component->name,
			component->type);

	return 0;
}

/* Change index of forward info */
static inline void
change_forward_index(int id)
{
	struct forward_info *info = &g_forward_info[id];
	if (info->ref_index == info->upd_index)
		info->ref_index = (info->upd_index+1)%SPP_INFO_AREA_MAX;
}
/**
 * Forwarding packets as forwarder or merger
 *
 * Behavior of forwarding is defined as core_info->type which is given
 * as an argument of void and typecasted to spp_config_info.
 */
int
spp_forward(int id)
{
	int cnt, num, buf;
	int nb_rx = 0;
	int nb_tx = 0;
	struct forward_info *info = &g_forward_info[id];
	struct forward_path *path = NULL;
	struct spp_port_info *rx;
	struct spp_port_info *tx;
	struct rte_mbuf *bufs[MAX_PKT_BURST];

	change_forward_index(id);
	path = &info->path[info->ref_index];
	num = path->num;

	for (cnt = 0; cnt < num; cnt++) {
		rx = &path->ports[cnt].rx;
		tx = &path->ports[cnt].tx;

		/* Receive packets */
		nb_rx = rte_eth_rx_burst(rx->dpdk_port, 0, bufs, MAX_PKT_BURST);
		if (unlikely(nb_rx == 0))
			continue;

#ifdef SPP_RINGLATENCYSTATS_ENABLE
		if (rx->iface_type == RING)
			spp_ringlatencystats_calculate_latency(rx->iface_no,
					bufs, nb_rx);

		if (tx->iface_type == RING)
			spp_ringlatencystats_add_time_stamp(tx->iface_no,
					bufs, nb_rx);
#endif /* SPP_RINGLATENCYSTATS_ENABLE */

		/* Send packets */
		if (tx->dpdk_port >= 0)
			nb_tx = rte_eth_tx_burst(tx->dpdk_port, 0, bufs, nb_rx);

		/* Discard remained packets to release mbuf */
		if (unlikely(nb_tx < nb_rx)) {
			for (buf = nb_tx; buf < nb_rx; buf++)
				rte_pktmbuf_free(bufs[buf]);
		}
	}
	return 0;
}

/* Merge/Forward get component status */
int
spp_forward_get_component_status(
		unsigned int lcore_id, int id,
		struct spp_iterate_core_params *params)
{
	int ret = -1;
	int cnt, num_tx;
	const char *component_type = NULL;
	struct forward_info *info = &g_forward_info[id];
	struct forward_path *path = &info->path[info->ref_index];
	struct spp_port_index rx_ports[RTE_MAX_ETHPORTS];
	struct spp_port_index tx_ports[RTE_MAX_ETHPORTS];

	if (unlikely(path->type == SPP_COMPONENT_UNUSE)) {
		RTE_LOG(ERR, FORWARD,
				"Component[%d] Not used. (status)(core = %d, type = %d)\n",
				id, lcore_id, path->type);
		return -1;
	}

	if (path->type == SPP_COMPONENT_MERGE)
		component_type = SPP_TYPE_MERGE_STR;
	else
		component_type = SPP_TYPE_FORWARD_STR;

	memset(rx_ports, 0x00, sizeof(rx_ports));
	for (cnt = 0; cnt < path->num; cnt++) {
		rx_ports[cnt].iface_type = path->ports[cnt].rx.iface_type;
		rx_ports[cnt].iface_no   = path->ports[cnt].rx.iface_no;
	}

	memset(tx_ports, 0x00, sizeof(tx_ports));
	num_tx = (path->num > 0)?1:0;
	tx_ports[0].iface_type = path->ports[0].tx.iface_type;
	tx_ports[0].iface_no   = path->ports[0].tx.iface_no;

	/* Set the information with the function specified by the command. */
	ret = (*params->element_proc)(
		params, lcore_id,
		path->name, component_type,
		path->num, rx_ports, num_tx, tx_ports);
	if (unlikely(ret != 0))
		return -1;

	return 0;
}