-
Notifications
You must be signed in to change notification settings - Fork 4
/
ncsi-netlink.py
executable file
·295 lines (251 loc) · 10.1 KB
/
ncsi-netlink.py
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
289
290
291
292
293
294
295
#!/usr/bin/python2
import ctypes
import socket
import argparse
from libnl.attr import nla_data, nla_parse, nla_for_each_nested, nla_get_string, nla_get_u16, nla_get_u32, NLA_NESTED, nla_parse_nested, nla_policy, nla_put_string, NLA_STRING, NLA_U16, NLA_U32, NLA_FLAG, nla_for_each_attr, nla_type, nla_is_nested, nla_get_flag, nla_put_u32, nla_len, nla_ok, nlattr, nla_next
from libnl.error import errmsg
from libnl.genl.genl import genl_connect, genlmsg_attrdata, genlmsg_attrlen, genlmsg_put, genlmsg_parse
from libnl.genl.ctrl import genl_ctrl_resolve
from libnl.linux_private.netlink import NLM_F_DUMP
from libnl.linux_private.genetlink import genlmsghdr
from libnl.handlers import NL_CB_CUSTOM, NL_CB_VALID, NL_SKIP
from libnl.msg import nlmsg_alloc, nlmsg_data, nlmsg_hdr
from libnl.nl import nl_recvmsgs_default, nl_send_auto
from libnl.socket_ import nl_socket_alloc, nl_socket_modify_cb
from libnl.misc import c_int
# NCSI Netlink commands and attributes - see include/uapi/linux/ncsi.h
NCSI_CMD_UNSPEC = 0
NCSI_CMD_PKG_INFO = 1
NCSI_CMD_SET_INTERFACE = 2
NCSI_CMD_CLEAR_INTERFACE = 3
NCSI_ATTR_UNSPEC = 0
NCSI_ATTR_IFINDEX = 1
NCSI_ATTR_PACKAGE_LIST = 2
NCSI_ATTR_PACKAGE_ID = 3
NCSI_ATTR_CHANNEL_ID = 4
NCSI_ATTR_MAX = 5
NCSI_PKG_UNSPEC = 0
NCSI_PKG_ATTR = 1
NCSI_PKG_ATTR_ID = 2
NCSI_PKG_ATTR_FORCED = 3
NCSI_PKG_ATTR_CHANNEL_LIST = 4
NCSI_PKG_ATTR_MAX = 5
NCSI_CHANNEL_ATTR_UNSPEC = 0
NCSI_CHANNEL_ATTR = 1
NCSI_CHANNEL_ATTR_ID = 2
NCSI_CHANNEL_ATTR_VERSION_MAJOR = 3
NCSI_CHANNEL_ATTR_VERSION_MINOR = 4
NCSI_CHANNEL_ATTR_VERSION_STR = 5
NCSI_CHANNEL_ATTR_LINK_STATE = 6
NCSI_CHANNEL_ATTR_ACTIVE = 7
NCSI_CHANNEL_ATTR_FORCED = 8
NCSI_CHANNEL_ATTR_VLAN_LIST = 9
NCSI_CHANNEL_ATTR_VLAN_ID = 10
NCSI_CHANNEL_ATTR_MAX = 11
NCSI_VLAN_UNSPEC = 0
NCSI_VLAN_INFO = 1
NCSI_VLAN_INFO_ID = 2
NCSI_VLAN_INFO_PROTO = 3
NCSI_VLAN_INFO_MAX = 4
# NCSI Netlink attribte policies
ncsi_policy = dict((i, None) for i in range(NCSI_ATTR_MAX))
ncsi_policy.update({
NCSI_ATTR_IFINDEX: nla_policy(type_=NLA_U32),
NCSI_ATTR_PACKAGE_LIST: nla_policy(type_=NLA_NESTED),
NCSI_ATTR_PACKAGE_ID: nla_policy(type_=NLA_U32),
NCSI_ATTR_CHANNEL_ID: nla_policy(type_=NLA_U32),
})
ncsi_package_policy = dict((i, None) for i in range(NCSI_PKG_ATTR_MAX))
ncsi_package_policy.update({
NCSI_PKG_ATTR: nla_policy(type_=NLA_NESTED),
NCSI_PKG_ATTR_ID: nla_policy(type_=NLA_U32),
NCSI_PKG_ATTR_FORCED: nla_policy(type_=NLA_FLAG),
NCSI_PKG_ATTR_CHANNEL_LIST: nla_policy(type_=NLA_NESTED),
})
ncsi_channel_policy = dict((i, None) for i in range(NCSI_CHANNEL_ATTR_MAX))
ncsi_channel_policy.update({
NCSI_CHANNEL_ATTR: nla_policy(type_=NLA_NESTED),
NCSI_CHANNEL_ATTR_ID: nla_policy(type_=NLA_U32),
NCSI_CHANNEL_ATTR_VERSION_MAJOR: nla_policy(type_=NLA_U32),
NCSI_CHANNEL_ATTR_VERSION_MINOR: nla_policy(type_=NLA_U32),
NCSI_CHANNEL_ATTR_VERSION_STR: nla_policy(type_=NLA_STRING),
NCSI_CHANNEL_ATTR_LINK_STATE: nla_policy(type_=NLA_U32),
NCSI_CHANNEL_ATTR_ACTIVE: nla_policy(type_=NLA_FLAG),
NCSI_CHANNEL_ATTR_FORCED: nla_policy(type_=NLA_FLAG),
NCSI_CHANNEL_ATTR_VLAN_LIST: nla_policy(type_=NLA_NESTED),
NCSI_CHANNEL_ATTR_VLAN_ID: nla_policy(type_=NLA_U16),
})
ncsi_vlan_policy = dict((i, None) for i in range(NCSI_VLAN_INFO_MAX))
ncsi_vlan_policy.update({
NCSI_VLAN_INFO: nla_policy(type_=NLA_NESTED),
NCSI_VLAN_INFO_ID: nla_policy(type_=NLA_U16),
NCSI_VLAN_INFO_PROTO: nla_policy(type_=NLA_U16),
})
# Dummy callback
def dump_callback(msg, _):
gnlh = genlmsghdr(nlmsg_data(nlmsg_hdr(msg)))
tb = dict((i, None) for i in range(NCSI_ATTR_MAX + 1))
nla_parse(tb, NCSI_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), None)
print(tb)
return NL_SKIP
# Callback to parse NCSI_CMD_PKG_INFO reply
def info_callback(msg, _):
nlh = nlmsg_hdr(msg)
gnlh = genlmsghdr(nlmsg_data(nlh))
tb = dict((i, None) for i in range(NCSI_ATTR_MAX + 1))
ret = genlmsg_parse(nlh, 0, tb, NCSI_ATTR_MAX, ncsi_policy)
if ret != 0:
reason = errmsg[abs(ret)]
print("genlmsg_parse returned {}, {}".format(ret, reason))
return ret
if not NCSI_ATTR_PACKAGE_LIST in tb:
print('No packages!')
return -1
rem = c_int()
for nla in nla_for_each_nested(tb[NCSI_ATTR_PACKAGE_LIST], rem):
ptb = dict()
ret = nla_parse_nested(ptb, NCSI_PKG_ATTR_MAX, nla, ncsi_package_policy)
if ret < 0:
print('Failed to parse package nest')
return ret
if NCSI_PKG_ATTR_ID in ptb:
print('package {}'.format(nla_get_u32(ptb[NCSI_PKG_ATTR_ID])))
else:
print('package (with no id?)')
if NCSI_PKG_ATTR_FORCED in ptb:
print('this package is forced')
print('----------')
crem = c_int()
for cnla in nla_for_each_nested(ptb[NCSI_PKG_ATTR_CHANNEL_LIST], crem):
ctb = dict()
ret = nla_parse_nested(ctb, NCSI_CHANNEL_ATTR_MAX, cnla, ncsi_channel_policy)
if ret < 0:
print('Failed to parse channel nest')
return ret
if NCSI_CHANNEL_ATTR_ID in ctb:
channel = nla_get_u32(ctb[NCSI_CHANNEL_ATTR_ID])
if NCSI_CHANNEL_ATTR_ACTIVE in ctb:
print('channel {} - active!'.format(channel))
else:
print('channel {}'.format(channel))
if NCSI_CHANNEL_ATTR_FORCED in ctb:
print('\tthis channel is forced')
else:
print('channel (with no id?)')
if NCSI_CHANNEL_ATTR_VERSION_MAJOR in ctb:
print('\tmajor version {}'.format(nla_get_u32(ctb[NCSI_CHANNEL_ATTR_VERSION_MAJOR])))
if NCSI_CHANNEL_ATTR_VERSION_MINOR in ctb:
print('\tminor version {}'.format(nla_get_u32(ctb[NCSI_CHANNEL_ATTR_VERSION_MINOR])))
if NCSI_CHANNEL_ATTR_VERSION_STR in ctb:
print('\tversion string {}'.format(nla_get_string(ctb[NCSI_CHANNEL_ATTR_VERSION_STR])))
if NCSI_CHANNEL_ATTR_LINK_STATE in ctb:
print('\tlink state {}'.format(nla_get_u32(ctb[NCSI_CHANNEL_ATTR_LINK_STATE])))
if NCSI_CHANNEL_ATTR_VLAN_LIST in ctb:
print('\tactive vlan ids:')
rrem = c_int()
vids = ctb[NCSI_CHANNEL_ATTR_VLAN_LIST]
vid = nlattr(nla_data(vids))
rrem.value = nla_len(vids)
while nla_ok(vid, rrem):
print('\t\t{}'.format(nla_get_u16(vid)))
vid = nla_next(vid, rrem)
return NL_SKIP
'''
Send an NCSI_CMD_SET_INTERFACE command. We can set either;
- A single package
- A single package and a single channel on that package (ie. a
particular port)
If neither package or channel ID are specified any previously set interface
is cleared via NCSI_CMD_CLEAR_INTERFACE.
'''
def ncsi_set_interface(ifindex, package, channel):
sk = nl_socket_alloc()
ret = genl_connect(sk)
if ret < 0:
return ret
driver_id = genl_ctrl_resolve(sk, b'NCSI')
if driver_id < 0:
return driver_id
msg = nlmsg_alloc()
if package or package and channel:
genlmsg_put(msg, 0, 0, driver_id, 0, 0, NCSI_CMD_SET_INTERFACE, 0)
ret = nla_put_u32(msg, NCSI_ATTR_PACKAGE_ID, int(package))
if channel:
ret = nla_put_u32(msg, NCSI_ATTR_CHANNEL_ID, int(channel))
else:
genlmsg_put(msg, 0, 0, driver_id, 0, 0, NCSI_CMD_CLEAR_INTERFACE, 0)
ret = nla_put_u32(msg, NCSI_ATTR_IFINDEX, ifindex)
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, dump_callback, None)
ret = nl_send_auto(sk, msg)
if ret < 0:
print("Failed to send message: {}".format(ret))
return ret
ret = nl_recvmsgs_default(sk) # blocks
if ret < 0:
reason = errmsg[abs(ret)]
print("recvmsg returned {}, {}".format(ret, reason))
'''
Send an NCSI_CMD_PKG_INFO command. If a package ID is not specified we pass the
NLM_F_DUMP flag to tell NCSI to list all packages.
'''
def ncsi_get_info(ifindex, package):
# Open socket to kernel
sk = nl_socket_alloc()
ret = genl_connect(sk)
if ret < 0:
print("Failed to open socket")
return -1
# Find NCSI
driver_id = genl_ctrl_resolve(sk, b'NCSI')
if driver_id < 0:
print("Could not resolve NCSI")
return -1;
# Setup up a Generic Netlink message
msg = nlmsg_alloc()
if package is None:
ret = genlmsg_put(msg, 0, 0, driver_id, 0, NLM_F_DUMP, NCSI_CMD_PKG_INFO, 0)
else:
ret = genlmsg_put(msg, 0, 0, driver_id, 0, 0, NCSI_CMD_PKG_INFO, 0)
nla_put_u32(msg, NCSI_ATTR_PACKAGE_ID, int(package))
if ret < 0:
reason = errmsg[abs(ret)]
print("genlmsg_put returned {}, {}".format(ret, reason))
return -1
nla_put_u32(msg, NCSI_ATTR_IFINDEX, ifindex)
# Add a callback function to the socket
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, info_callback, None)
ret = nl_send_auto(sk, msg)
if ret < 0:
print("Failed to send message: {}".format(ret))
return ret
ret = nl_recvmsgs_default(sk) # blocks
if ret < 0:
reason = errmsg[abs(ret)]
print("recvmsg returned {}, {}".format(ret, reason))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--info",
help="retrieve info about NCSI topology",
action="store_true")
parser.add_argument("-s", "--set",
help="set a specific package / channel",
action="store_true")
parser.add_argument("-p", "--package",
help="specify a package")
parser.add_argument("-c", "--channel",
help="specify a channel")
# On a Witherspoon BMC eth0 is 2
parser.add_argument("-x", "--index", type=int, default=2,
help="specify device ifindex")
args = parser.parse_args()
if args.info:
ncsi_get_info(args.index, args.package)
return
if args.channel and not args.package:
print('You must specify a package id with a channel id')
return -1
if args.set:
ncsi_set_interface(args.index, args.package, args.channel)
return
if __name__ == '__main__':
main()