1aa97dd1cSKonstantin Ananyev/*
2aa97dd1cSKonstantin Ananyev * Copyright (c) 2016  Intel Corporation.
3aa97dd1cSKonstantin Ananyev * Licensed under the Apache License, Version 2.0 (the "License");
4aa97dd1cSKonstantin Ananyev * you may not use this file except in compliance with the License.
5aa97dd1cSKonstantin Ananyev * You may obtain a copy of the License at:
6aa97dd1cSKonstantin Ananyev *
7aa97dd1cSKonstantin Ananyev *     http://www.apache.org/licenses/LICENSE-2.0
8aa97dd1cSKonstantin Ananyev *
9aa97dd1cSKonstantin Ananyev * Unless required by applicable law or agreed to in writing, software
10aa97dd1cSKonstantin Ananyev * distributed under the License is distributed on an "AS IS" BASIS,
11aa97dd1cSKonstantin Ananyev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12aa97dd1cSKonstantin Ananyev * See the License for the specific language governing permissions and
13aa97dd1cSKonstantin Ananyev * limitations under the License.
14aa97dd1cSKonstantin Ananyev */
15aa97dd1cSKonstantin Ananyev#include <rte_malloc.h>
16aa97dd1cSKonstantin Ananyev#include <rte_errno.h>
17aa97dd1cSKonstantin Ananyev
18aa97dd1cSKonstantin Ananyev#include "tcp_stream.h"
19aa97dd1cSKonstantin Ananyev#include "tcp_rxq.h"
20aa97dd1cSKonstantin Ananyev
21aa97dd1cSKonstantin Ananyev#define	OFO_FRACTION	4
22aa97dd1cSKonstantin Ananyev
23aa97dd1cSKonstantin Ananyev#define OFO_DB_MAX	0x20U
24aa97dd1cSKonstantin Ananyev
25aa97dd1cSKonstantin Ananyev#define OFODB_OBJ_MIN	8U
26aa97dd1cSKonstantin Ananyev#define OFODB_OBJ_MAX	0x20U
27aa97dd1cSKonstantin Ananyev
28aa97dd1cSKonstantin Ananyev#define OFO_OBJ_MAX	(OFODB_OBJ_MAX * OFO_DB_MAX)
29aa97dd1cSKonstantin Ananyev
3047eb00f2SKonstantin Ananyevstatic uint32_t
3147eb00f2SKonstantin Ananyevcalc_ofo_size(uint32_t nobj, uint32_t ndb)
32aa97dd1cSKonstantin Ananyev{
3347eb00f2SKonstantin Ananyev	uint32_t dsz, osz, sz;
3447eb00f2SKonstantin Ananyev	const struct ofo *ofo = NULL;
3547eb00f2SKonstantin Ananyev
3647eb00f2SKonstantin Ananyev	osz = sizeof(*ofo) + sizeof(ofo->db[0]) * ndb;
3747eb00f2SKonstantin Ananyev	dsz = sizeof(ofo->db[0].obj[0]) * nobj * ndb;
3847eb00f2SKonstantin Ananyev	sz = osz + dsz;
3947eb00f2SKonstantin Ananyev	return sz;
40aa97dd1cSKonstantin Ananyev}
41aa97dd1cSKonstantin Ananyev
4247eb00f2SKonstantin Ananyevvoid
4347eb00f2SKonstantin Ananyevtcp_ofo_calc_elems(uint32_t nbufs, uint32_t *nobj, uint32_t *ndb, uint32_t *sz)
44aa97dd1cSKonstantin Ananyev{
45aa97dd1cSKonstantin Ananyev	uint32_t n, nd, no;
46aa97dd1cSKonstantin Ananyev
47aa97dd1cSKonstantin Ananyev	n = nbufs / OFO_FRACTION;
48aa97dd1cSKonstantin Ananyev	n = RTE_MAX(n, OFODB_OBJ_MIN);
49aa97dd1cSKonstantin Ananyev	n = RTE_MIN(n, OFO_OBJ_MAX);
50aa97dd1cSKonstantin Ananyev
51aa97dd1cSKonstantin Ananyev	no = OFODB_OBJ_MIN / 2;
52aa97dd1cSKonstantin Ananyev	do {
53aa97dd1cSKonstantin Ananyev		no *= 2;
54aa97dd1cSKonstantin Ananyev		nd = n / no;
55aa97dd1cSKonstantin Ananyev	} while (nd > OFO_DB_MAX);
56aa97dd1cSKonstantin Ananyev
57aa97dd1cSKonstantin Ananyev	*nobj = no;
58aa97dd1cSKonstantin Ananyev	*ndb = nd;
5947eb00f2SKonstantin Ananyev	*sz = calc_ofo_size(no, nd);
60aa97dd1cSKonstantin Ananyev}
61aa97dd1cSKonstantin Ananyev
6247eb00f2SKonstantin Ananyevvoid
6347eb00f2SKonstantin Ananyevtcp_ofo_init(struct ofo *ofo, uint32_t nobj, uint32_t ndb)
64aa97dd1cSKonstantin Ananyev{
6547eb00f2SKonstantin Ananyev	uint32_t i;
66aa97dd1cSKonstantin Ananyev	struct rte_mbuf **obj;
67aa97dd1cSKonstantin Ananyev
68aa97dd1cSKonstantin Ananyev	obj = (struct rte_mbuf **)&ofo->db[ndb];
69aa97dd1cSKonstantin Ananyev	for (i = 0; i != ndb; i++) {
70aa97dd1cSKonstantin Ananyev		ofo->db[i].nb_max = nobj;
71aa97dd1cSKonstantin Ananyev		ofo->db[i].obj = obj + i * nobj;
72aa97dd1cSKonstantin Ananyev	}
73aa97dd1cSKonstantin Ananyev
74aa97dd1cSKonstantin Ananyev	ofo->nb_max = ndb;
75aa97dd1cSKonstantin Ananyev}
76aa97dd1cSKonstantin Ananyev
77