tcp_ofo.c revision aa97dd1c
1/*
2 * Copyright (c) 2016  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <rte_malloc.h>
16#include <rte_errno.h>
17
18#include "tcp_stream.h"
19#include "tcp_rxq.h"
20
21#define	OFO_FRACTION	4
22
23#define OFO_DB_MAX	0x20U
24
25#define OFODB_OBJ_MIN	8U
26#define OFODB_OBJ_MAX	0x20U
27
28#define OFO_OBJ_MAX	(OFODB_OBJ_MAX * OFO_DB_MAX)
29
30void
31tcp_ofo_free(struct ofo *ofo)
32{
33	rte_free(ofo);
34}
35
36static void
37calc_ofo_elems(uint32_t nbufs, uint32_t *nobj, uint32_t *ndb)
38{
39	uint32_t n, nd, no;
40
41	n = nbufs / OFO_FRACTION;
42	n = RTE_MAX(n, OFODB_OBJ_MIN);
43	n = RTE_MIN(n, OFO_OBJ_MAX);
44
45	no = OFODB_OBJ_MIN / 2;
46	do {
47		no *= 2;
48		nd = n / no;
49	} while (nd > OFO_DB_MAX);
50
51	*nobj = no;
52	*ndb = nd;
53}
54
55struct ofo *
56tcp_ofo_alloc(uint32_t nbufs, int32_t socket)
57{
58	uint32_t i, ndb, nobj;
59	size_t dsz, osz, sz;
60	struct ofo *ofo;
61	struct rte_mbuf **obj;
62
63	calc_ofo_elems(nbufs, &nobj, &ndb);
64	osz = sizeof(*ofo) + sizeof(ofo->db[0]) * ndb;
65	dsz = sizeof(ofo->db[0].obj[0]) * nobj * ndb;
66	sz = osz + dsz;
67
68	ofo = rte_zmalloc_socket(NULL, sz, RTE_CACHE_LINE_SIZE, socket);
69	if (ofo == NULL) {
70		TCP_LOG(ERR, "%s: allocation of %zu bytes on socket %d "
71			"failed with error code: %d\n",
72			__func__, sz, socket, rte_errno);
73		return NULL;
74	}
75
76	obj = (struct rte_mbuf **)&ofo->db[ndb];
77	for (i = 0; i != ndb; i++) {
78		ofo->db[i].nb_max = nobj;
79		ofo->db[i].obj = obj + i * nobj;
80	}
81
82	ofo->nb_max = ndb;
83	return ofo;
84}
85
86