-
Notifications
You must be signed in to change notification settings - Fork 1
/
ports.h
executable file
·70 lines (63 loc) · 2.11 KB
/
ports.h
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
#ifndef __scable_ports__
#define __scable_ports__
#include "rte.h"
#include "port.h"
class Ports {
private:
typedef std::map<uint16_t, Port> PortsMap;
PortsMap ports;
struct rte_mempool *pktmbufPool;
Ports() {
}
Rte::Name mbufName;
public:
static const int NB_MBUF = 8192;
static Ports *create() {
auto ports = new Ports();
/* create the mbuf pool */
ports->pktmbufPool = rte_pktmbuf_pool_create(ports->mbufName("port:mbuf:"),
NB_MBUF, 32, 0,
RTE_MBUF_DEFAULT_BUF_SIZE,
rte_socket_id());
if (ports->pktmbufPool == 0) {
delete ports;
LOG(ERROR) << "Cannot init mbuf pool";
return 0;
}
uint8_t portCount = rte_eth_dev_count();
if (portCount == 0) {
LOG(ERROR) << "No ports defined you need exact two";
return 0;
}
if (portCount != 2) {
LOG(ERROR) << "not the right # of ports defined you need exact two";
return 0;
}
for (int i = 0; i < portCount; ++i) {
ports->addPort(i);
}
return ports;
}
struct rte_mempool *getPktMbufPool() const {
return pktmbufPool;
}
Port *find(int portId) {
auto it = ports.find(portId);
if (it == ports.end()) {
return 0;
}
return &(it->second);
}
void addPort(int portId) {
auto it = ports.find(portId);
if (it == ports.end()) {
PortsMap::iterator hint = ports.lower_bound(portId);
it = ports.insert(hint, PortsMap::value_type(portId, Port(portId, *this)));
it->second.start();
LOG(INFO) << "Started Port with id:" << it->second.getId();
} else {
LOG(WARNING) << "Try to add the same port twice!";
}
}
};
#endif