forked from pktgen/Pktgen-DPDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pktgen.lua
278 lines (229 loc) · 7.13 KB
/
Pktgen.lua
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
-- SPDX-License-Identifier: BSD-3-Clause
-- Create some short cuts to the real functions.
gsub = string.gsub
gmatch = string.gmatch
strrep = string.rep
strsub = string.sub
strfmt = string.format
strmatch = string.match
strrep = string.rep
strfind = string.find
strlen = string.len
tolower = string.lower
unlink = os.remove
system = os.execute
tinsert = table.insert
tgetn = table.getn
tconcat = table.concat
-- ===========================================================================
-- getPwd - Get the current working directory path.
-- Works for both dos and cygwin shell, which may emit an error if one failes.
--
function getPwd( func )
local s = syscmd("pwd", func);
if ( s == nil ) then
s = syscmd("sh pwd", func);
end
return s;
end
-- ===========================================================================
-- d2u - convert dos backslashes to unix forward slashes.
--
function d2u( str )
return gsub(str or "", "\\", "/");
end
-- ===========================================================================
-- u2d - convert unix forward slashes to dos backslashes.
--
function u2d( str )
return gsub(str or "", "/", "\\");
end
-- ===========================================================================
-- str2tbl - convert a string to a table.
--
function str2tbl( str )
local t = {};
local s;
for s in gmatch(str or "", "(.-)\n") do
tinsert(t, s);
end
return t
end
-- ===========================================================================
-- trims the string of beginning and trailing spaces and tabs.
--
function trim(txt) return gsub(txt, "%s*(.-)%s*$", "%1", 1); end
-- ===========================================================================
-- A formatted output routine just like the real printf.
--
function printf(...) io.write(strfmt(...)); io.flush(); end
-- ===========================================================================
-- returns the table size or number of items in table.
--
function getn( t )
local i = 0;
if ( (t ~= nil) and (type(t) == "table") ) then
i = tgetn(t);
if ( i > 0 ) then
return i;
end
for k in pairs(t) do
i = i + 1;
end
end
return i;
end
-- ===========================================================================
-- returns the 'basename' and the 'basedir'
--
function basename(filename)
local fn, dn;
-- Convert the '\' to '/' in the path name.
filename = d2u(filename);
fn = gsub(filename, "(.*/)(.*)", "%2") or filename;
dn = gsub(filename, "(.*/)(.*)", "%1")
dn = strsub(dn, 1, -2);
return fn, dn;
end
-- ===========================================================================
-- Default routine to read data from syscmd function.
--
local function __doRead(fn)
local data, f;
-- Open and read all of the data.
f = assert(io.open(fn));
data = f:read("*all");
f:close();
unlink(fn);
return data;
end
-- ===========================================================================
-- Execute the system command return the command output if needed.
--
function syscmd( cmd, funcPtr )
local tmpFile = "syscmd_tmp";
system( cmd .. " > " .. tmpFile );
funcPtr = funcPtr or __doRead;
return funcPtr(tmpFile); -- tmpFile is removed by the function.
end
-- ===========================================================================
-- Execute the string and return true/false.
--
function isTrue(f)
local s;
if ( f == nil ) then
return 0;
end
s = "if ( "..f.." ) then return 1 else return 0 end";
return assert(loadstring(s))();
end
-- ===========================================================================
-- Output a message and return.
--
function msg(m, ...)
if ( m ~= nil ) then io.write("++ "..strfmt(m, ...)); io.flush(); end
end
-- ===========================================================================
-- Display an error message and exit.
--
function errmsg(m, ...)
if ( m ~= nil ) then printf("** %s", strfmt(m, ...)); end
os.exit(1);
end
-- ===========================================================================
-- Output a 'C' like block comment.
--
function cPrintf(m, ...)
printf("/* ");
io.write(strfmt(m, ...));
printf(" */\n");
end
-- ===========================================================================
-- Output a 'C' like comment.
--
function comment(msg)
printf("/* %s */\n", msg or "ooops");
end
-- Standard set of functions for normal operation.
--
-----------------------------------------------------------------------------
-- serializeIt - Convert a variable to text or its type of variable.
--
local function serializeIt(v)
local s;
local t = type(v);
if (t == "number") then
s = tostring(v);
elseif (t == "table") then
s = tostring(v);
elseif (t == "string") then
s = strfmt("%q", v);
elseif (t == "boolean") then
s = tostring(v);
elseif (t == "function") then
s = strfmt("()");
elseif (t == "userdata") then
s = tostring(v);
elseif (t == "nil") then
s = "nil";
else
s = strfmt("<%s>", tostring(v));
end
return s;
end
-----------------------------------------------------------------------------
-- Serialize a value
-- k - is the variable name string.
-- o - is the orignal variable name for tables.
-- v - the value of the variable.
-- saved - is the saved table to detect loops.
-- tab - is the current tab depth.
--
local function doSerialize(k, o, v, saved, tab)
local s, t;
local space = function(t) return strrep(" ", t); end;
tab = tab or 0;
t = type(v);
saved = saved or {};
if (t == "table") then
if ( saved[v] ~= nil ) then
return strfmt("%s[%s] = %s,\n", space(tab), serializeIt(o), saved[v]);
else
local kk, vv, mt;
saved[v] = k;
if ( tab == 0 ) then
s = strfmt("%s%s = {\n", space(tab), tostring(k));
else
s = strfmt("%s[%s] = {\n", space(tab), serializeIt(o));
end
for kk,vv in pairs(v) do
local fn = strfmt("%s[%s]", tostring(k), serializeIt(kk));
s = s .. doSerialize(fn, kk, vv, saved, tab+2);
end
if ( tab == 0 ) then
return s .. strfmt("%s}\n", space(tab));
else
return s .. strfmt("%s},\n", space(tab));
end
end
else
return strfmt("%s[%s] = %s,\n", space(tab), serializeIt(o), serializeIt(v));
end
end
-----------------------------------------------------------------------------
-- serialize - For a given key serialize the global variable.
-- k is a string for display and t is the table to display.
-- e.g. printf(serialize("foo", { ["bar"] = "foobar" } ));
--
function serialize(k, t)
if ( k == nil ) then
k = "Globals";
t = _G; -- Dump out globals
end
if ( t == nil ) then
t = _G;
end
return doSerialize(k, k, t, {}, 0);
end
function prints(k, t) io.write(serialize(k, t)); io.flush(); end
function sleep(t) pktgen.delay(t * 1000); end