This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fw.ts
288 lines (259 loc) · 8.63 KB
/
fw.ts
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { Histogram } from "@usnistgov/ndn-dpdk/cmd/ndndpdk-hrlog2histogram/mod";
import { InitConfig } from "@usnistgov/ndn-dpdk/cmd/ndnfw-dpdk/mod";
import * as stat from "@usnistgov/ndn-dpdk/core/running_stat/mod";
import { Counters as FaceCounters, FaceId } from "@usnistgov/ndn-dpdk/iface/mod";
import PProgress from "p-progress";
import smallestPowerOfTwo from "smallest-power-of-two";
import { env, NetifInfo } from "./config";
import { Host } from "./host";
import { RuntimeDir } from "./runtime-dir";
const tableCntKeys = {
nPitInsert: true,
nPitFound: true,
nCsHits: true,
nCsMisses: true,
};
const pktCntKeys = {
RxFrames: true,
RxOctets: true,
RxInterests: true,
RxData: true,
RxNacks: true,
TxFrames: true,
TxOctets: true,
TxInterests: true,
TxData: true,
TxNacks: true,
};
const latencyCntKeys = {
InterestLatency: true,
DataLatency: true,
NackLatency: true,
};
export interface FwCounters {
fw: Array<Record<keyof typeof tableCntKeys, number>>;
face: Record<string, Pick<FaceCounters, keyof typeof pktCntKeys|keyof typeof latencyCntKeys>>;
total: Record<keyof typeof tableCntKeys|keyof typeof pktCntKeys, number> & Record<keyof typeof latencyCntKeys, stat.Snapshot>;
}
function makeEmptyFwCounters(): FwCounters {
return {
face: {},
fw: [],
total: {
...Object.fromEntries((Object.keys(tableCntKeys)).map((key) => [key, 0])),
...Object.fromEntries((Object.keys(pktCntKeys)).map((key) => [key, 0])),
...Object.fromEntries((Object.keys(latencyCntKeys)).map((key) => [key, stat.empty])),
} as any,
};
}
export class FwCountersSnapshot {
constructor(private readonly a: FwCounters) {
}
public until(c: FwCounters): FwCounters {
const b = makeEmptyFwCounters();
for (let fwIndex = 0; fwIndex < c.fw.length; ++fwIndex) {
const fc = c.fw[fwIndex];
const fa = this.a.fw[fwIndex];
const fb: any = {};
for (const key of Object.keys(tableCntKeys) as Iterable<keyof typeof tableCntKeys>) {
fb[key] = fc[key] - fa[key];
b.total[key] += fb[key];
}
b.fw.push(fb);
}
for (const portIndex of Object.keys(c.face)) {
const fc = c.face[portIndex];
const fa = this.a.face[portIndex];
const fb: any = {};
for (const key of Object.keys(pktCntKeys) as Iterable<keyof typeof pktCntKeys>) {
fb[key] = fc[key] - fa[key];
b.total[key] += fb[key];
}
for (const key of Object.keys(latencyCntKeys) as Iterable<keyof typeof latencyCntKeys>) {
fb[key] = stat.sub(fc[key], fa[key]);
b.total[key] = stat.add(b.total[key], fb[key]);
}
b.face[portIndex] = fb;
}
return b;
}
}
export type HrlogHistograms = Histogram[];
/** Control a host running NDN-DPDK forwarder. */
export class Forwarder extends Host {
public constructor(runtimeDir: RuntimeDir, mgmtUri: string = env.MGMT_FW, netifs: readonly NetifInfo[] = env.IF_FW) {
super(runtimeDir, "fw", mgmtUri, netifs);
}
public options = {
PitCap: 50000, // this allows one million I/s at 50ms RTT
CsCapMd: 32768,
CsCapMi: 32768,
enableHrlog: false,
saveHrlog: false,
};
public initConfigOptions: NonNullable<Parameters<Host["buildInitConfig"]>[0]> = {};
private faces = new Map<string, number>(); // portIndex=>face
private ndtValues = new Map<number, number>(); // Index=>Value
/**
* Allocate forwarding threads.
* @param numa NUMA socket.
* @param count number of forwarding threads.
* @throws insufficient lcores.
*/
public allocFwds(numa: number, count: number) {
for (let i = 0; i < count; ++i) {
this.lcores.add("FWD", this.cpuList.take(numa));
}
}
/** Count number of forwarding threads among all NUMA sockets. */
public countFwds(): number {
return this.lcores.list("FWD").length;
}
/** Build init-config including forwarder-specific sections. */
protected buildInitConfig(): InitConfig {
const {
PitCap,
CsCapMd,
CsCapMi,
} = this.options;
const PcctCapacity = smallestPowerOfTwo(CsCapMd * 2 + CsCapMi + PitCap) - 1;
return {
...super.buildInitConfig(this.initConfigOptions),
Ndt: {
PrefixLen: 2,
IndexBits: 16,
SampleFreq: 16,
},
Fib: {
MaxEntries: 4194303,
NBuckets: 262144,
StartDepth: 8,
},
Fwdp: {
FwdInterestQueue: {
DequeueBurstSize: 32,
},
FwdDataQueue: {
DequeueBurstSize: 64,
},
FwdNackQueue: {
DequeueBurstSize: 64,
},
LatencySampleFreq: 16,
Suppress: {
Min: 200e6,
Max: 200e6,
},
PcctCapacity,
CsCapMd,
CsCapMi,
},
};
}
/** Start the forwarder. */
public async start() {
this.runtimeDir.writeFile("fw.init-config.json", this.buildInitConfig());
return this.startImpl("fw-start.sh", { INITCONFIG: "fw.init-config.json" }, { FWLOG: "fw.log" });
}
/**
* Create an Ethernet face.
* @param index index letter of the port.
*/
public async createFace(index: string): Promise<FaceId> {
let faceId = this.faces.get(index);
if (typeof faceId === "undefined") {
const netif = this.ethPorts.get(index);
if (!netif) {
throw new Error(`port ${index} not declared`);
}
const face = await this.mgmt.request("Face", "Create", { Scheme: "ether", Port: netif.pci });
faceId = face.Id;
this.faces.set(index, faceId);
}
return faceId;
}
/**
* Write an NDT record.
* In case the same NDT index (truncated name hash) has been explicitly set to a different value
* (forwarding thread number), this situation is logged in fw.ndt-conflicts.ndjson file.
*/
public async setNdtRecord(name: string, value: number) {
const { Index: index } = await this.mgmt.request("Ndt", "Update", {
Name: name,
Value: value,
});
const oldValue = this.ndtValues.get(index);
if (typeof oldValue !== "undefined" && oldValue !== value) {
this.runtimeDir.writeNdjson("fw.ndt-conflicts.ndjson", { index, name, oldValue, newValue: value });
}
this.ndtValues.set(index, value);
}
/** Write a FIB entry. */
public async setFibEntry(name: string, nexthops: readonly number[]) {
await this.mgmt.request("Fib", "Insert", {
Name: name,
Nexthops: nexthops.concat([]),
});
}
/** Populate the FIB with many entries that are not expected to be used. */
public fillFib: (count: number, template?: string) => PProgress<void> =
PProgress.fn(async (count: number, arg2: any, arg3: any) => {
const [template, progress]: [string, PProgress.ProgressNotifier] =
typeof arg2 === "function" ? ["/65535=Z/%", arg2] : [arg2, arg3];
for (let i = 0; i < count; ++i) {
await this.setFibEntry(template.replace(/%/g, `${i}`), [1]);
progress(i / count);
}
});
private async readCounters(): Promise<FwCounters> {
const cnt = makeEmptyFwCounters();
for (let fwIndex = 0, nFwds = this.countFwds(); fwIndex < nFwds; ++fwIndex) {
const [pitCnt, csCnt] = await Promise.all([
this.mgmt.request("DpInfo", "Pit", { Index: fwIndex }),
this.mgmt.request("DpInfo", "Cs", { Index: fwIndex }),
]);
cnt.fw.push({
nPitFound: pitCnt.NFound,
nPitInsert: pitCnt.NInsert,
nCsHits: csCnt.NHits,
nCsMisses: csCnt.NMisses,
});
}
for (const [portIndex, faceId] of this.faces) {
const faceInfo = await this.mgmt.request("Face", "Get", { Id: faceId });
cnt.face[portIndex] = faceInfo.Counters;
}
return cnt;
}
public async snapshotCounters(): Promise<FwCountersSnapshot> {
return new FwCountersSnapshot(await this.readCounters());
}
public async readCountersSince(since: FwCountersSnapshot): Promise<FwCounters> {
return since.until(await this.readCounters());
}
/**
* Collect high resolution per-packet logs.
* @returns filename
*/
public async startHrlog(): Promise<string|undefined> {
if (!this.options.enableHrlog) {
return undefined;
}
const localFile = `${Date.now()}.hrlog`;
const remoteFile = await this.hostDir.create(localFile, { download: this.options.saveHrlog, touch: true });
await this.mgmt.request("Hrlog", "Start", {
Filename: remoteFile,
});
return localFile;
}
public async stopHrlog(localFile: string|undefined): Promise<HrlogHistograms|undefined> {
if (!localFile) {
return undefined;
}
const remoteFile = await this.hostDir.create(localFile);
await this.mgmt.request("Hrlog", "Stop", {
Filename: remoteFile,
});
return JSON.parse(await this.ssh.exec(`ndndpdk-hrlog2histogram -f ${remoteFile}`));
}
}