-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathqueue_inc.cc
More file actions
146 lines (125 loc) · 4.97 KB
/
queue_inc.cc
File metadata and controls
146 lines (125 loc) · 4.97 KB
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
// Copyright (c) 2014-2016, The Regents of the University of California.
// Copyright (c) 2016-2017, Nefeli Networks, Inc.
// 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 names of the copyright holders nor the names of their
// 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 HOLDER 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 "queue_inc.h"
#include "../port.h"
#include "../utils/format.h"
const Commands QueueInc::cmds = {{"set_burst", "QueueIncCommandSetBurstArg",
MODULE_CMD_FUNC(&QueueInc::CommandSetBurst),
Command::THREAD_SAFE}};
CommandResponse QueueInc::Init(const bess::pb::QueueIncArg &arg) {
const char *port_name;
task_id_t tid;
CommandResponse err;
burst_ = bess::PacketBatch::kMaxBurst;
if (!arg.port().length()) {
return CommandFailure(EINVAL, "Field 'port' must be specified");
}
port_name = arg.port().c_str();
qid_ = arg.qid();
const auto &it = PortBuilder::all_ports().find(port_name);
if (it == PortBuilder::all_ports().end()) {
return CommandFailure(ENODEV, "Port %s not found", port_name);
}
port_ = it->second;
burst_ = bess::PacketBatch::kMaxBurst;
if (arg.prefetch()) {
prefetch_ = 1;
}
node_constraints_ = port_->GetNodePlacementConstraint();
tid = RegisterTask((void *)(uintptr_t)qid_);
if (tid == INVALID_TASK_ID)
return CommandFailure(ENOMEM, "Context creation failed");
int ret = port_->AcquireQueues(reinterpret_cast<const module *>(this),
PACKET_DIR_INC, &qid_, 1);
if (ret < 0) {
return CommandFailure(-ret);
}
return CommandSuccess();
}
void QueueInc::DeInit() {
if (port_) {
port_->ReleaseQueues(reinterpret_cast<const module *>(this), PACKET_DIR_INC,
&qid_, 1);
}
}
std::string QueueInc::GetDesc() const {
return bess::utils::Format("%s:%hhu/%s", port_->name().c_str(), qid_,
port_->port_builder()->class_name().c_str());
}
struct task_result QueueInc::RunTask(Context *ctx, bess::PacketBatch *batch,
void *arg) {
if (!port_->conf().admin_up) {
return {.block = true, .packets = 0, .bits = 0};
}
const queue_t qid = (queue_t)(uintptr_t)arg;
auto &qstats = port_->queue_stats_[PACKET_DIR_INC][qid];
const int burst = ACCESS_ONCE(burst_);
const int pkt_overhead = 24;
uint32_t cnt = port_->RecvPackets(qid, batch->pkts(), burst);
batch->set_cnt(cnt);
qstats.requested_hist[burst]++;
qstats.actual_hist[cnt]++;
qstats.diff_hist[burst - cnt]++;
if (cnt == 0) {
return {.block = true, .packets = 0, .bits = 0};
}
// NOTE: we cannot skip this step since it might be used by scheduler.
uint64_t received_bytes = 0;
if (prefetch_) {
for (uint32_t i = 0; i < cnt; i++) {
received_bytes += batch->pkts()[i]->total_len();
rte_prefetch0(batch->pkts()[i]->head_data());
}
} else {
for (uint32_t i = 0; i < cnt; i++) {
received_bytes += batch->pkts()[i]->total_len();
}
}
if (!(port_->GetFeatures().offloadIncStats)) {
qstats.packets += cnt;
qstats.bytes += received_bytes;
}
RunNextModule(ctx, batch);
return {.block = false,
.packets = cnt,
.bits = (received_bytes + cnt * pkt_overhead) * 8};
}
CommandResponse QueueInc::CommandSetBurst(
const bess::pb::QueueIncCommandSetBurstArg &arg) {
if (arg.burst() > bess::PacketBatch::kMaxBurst) {
return CommandFailure(EINVAL, "burst size must be [0,%zu]",
bess::PacketBatch::kMaxBurst);
} else {
burst_ = arg.burst();
return CommandSuccess();
}
}
ADD_MODULE(QueueInc, "queue_inc",
"receives packets from a port via a specific queue")