Skip to content

Commit 5947d2c

Browse files
committed
fixup! luaskb: add priority getter and setter
Signed-off-by: Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com>
1 parent e61e9b8 commit 5947d2c

1 file changed

Lines changed: 78 additions & 26 deletions

File tree

lib/luaskb.c

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,32 @@ static int luaskb_forward(lua_State *L)
195195
return 0;
196196
}
197197

198-
/***
199-
* Gets or sets skb->priority, used by TC classifiers to select an HTB class.
200-
* @function priority
201-
* @tparam[opt] integer priority TC handle in TC_H_MAKE(major, minor) format
202-
* @treturn integer current priority, or nothing if setting
203-
*/
204-
static int luaskb_priority(lua_State *L)
198+
static int luaskb_getmark(lua_State *L)
199+
{
200+
luaskb_t *lskb = luaskb_check(L, 1);
201+
lua_pushinteger(L, lskb->skb->mark);
202+
return 1;
203+
}
204+
205+
static int luaskb_setmark(lua_State *L)
206+
{
207+
luaskb_t *lskb = luaskb_check(L, 1);
208+
lskb->skb->mark = (u32)luaL_checkinteger(L, 3); /* __newindex: key=2, val=3 */
209+
return 0;
210+
}
211+
212+
static int luaskb_getpriority(lua_State *L)
205213
{
206-
luaskb_t *lskb = luaskb_check(L, 1);
207-
struct sk_buff *skb = lskb->skb;
208-
209-
if (lua_gettop(L) >= 2) {
210-
skb->priority = (u32)luaL_checkinteger(L, 2);
211-
return 0;
212-
}
213-
lua_pushinteger(L, skb->priority);
214-
return 1;
214+
luaskb_t *lskb = luaskb_check(L, 1);
215+
lua_pushinteger(L, lskb->skb->priority);
216+
return 1;
217+
}
218+
219+
static int luaskb_setpriority(lua_State *L)
220+
{
221+
luaskb_t *lskb = luaskb_check(L, 1);
222+
lskb->skb->priority = (u32)luaL_checkinteger(L, 3);
223+
return 0;
215224
}
216225

217226
static int luaskb_copy(lua_State *L);
@@ -225,24 +234,67 @@ static void luaskb_release(void *private)
225234
luadata_close(lskb->data);
226235
}
227236

237+
static const luaL_Reg luaskb_getters[] = {
238+
{"mark", luaskb_getmark},
239+
{"priority", luaskb_getpriority},
240+
{NULL, NULL}
241+
};
242+
243+
static const luaL_Reg luaskb_setters[] = {
244+
{"mark", luaskb_setmark},
245+
{"priority", luaskb_setpriority},
246+
{NULL, NULL}
247+
};
248+
249+
static int luaskb_index(lua_State *L)
250+
{
251+
lua_getmetatable(L, 1);
252+
lua_pushvalue(L, 2);
253+
lua_rawget(L, -2);
254+
if (!lua_isnil(L, -1))
255+
return 1;
256+
lua_pop(L, 2);
257+
258+
const char *key = luaL_checkstring(L, 2);
259+
for (const luaL_Reg *f = luaskb_getters; f->name; f++)
260+
if (strcmp(f->name, key) == 0)
261+
return f->func(L);
262+
263+
lua_pushnil(L);
264+
return 1;
265+
}
266+
267+
static int luaskb_newindex(lua_State *L)
268+
{
269+
const char *key = luaL_checkstring(L, 2);
270+
for (const luaL_Reg *f = luaskb_setters; f->name; f++)
271+
if (strcmp(f->name, key) == 0)
272+
return f->func(L);
273+
274+
luaL_error(L, "skb field '%s' is read-only or does not exist", key);
275+
return 0;
276+
}
277+
228278
static const luaL_Reg luaskb_lib[] = {
229279
{NULL, NULL}
230280
};
231281

232282
static const luaL_Reg luaskb_mt[] = {
233-
{"__gc", lunatik_deleteobject},
234-
{"__len", luaskb_len},
235-
{"ifindex", luaskb_ifindex},
236-
{"vlan", luaskb_vlan},
237-
{"data", luaskb_data},
238-
{"resize", luaskb_resize},
239-
{"checksum", luaskb_checksum},
240-
{"forward", luaskb_forward},
241-
{"copy", luaskb_copy},
242-
{"priority", luaskb_priority},
283+
{"__gc", lunatik_deleteobject},
284+
{"__len", luaskb_len},
285+
{"__index", luaskb_index},
286+
{"__newindex", luaskb_newindex},
287+
{"ifindex", luaskb_ifindex},
288+
{"vlan", luaskb_vlan},
289+
{"data", luaskb_data},
290+
{"resize", luaskb_resize},
291+
{"checksum", luaskb_checksum},
292+
{"forward", luaskb_forward},
293+
{"copy", luaskb_copy},
243294
{NULL, NULL}
244295
};
245296

297+
246298
LUNATIK_OPENER(skb);
247299
static const lunatik_class_t luaskb_class = {
248300
.name = "skb",

0 commit comments

Comments
 (0)