Lines Matching full:self

80   def __init__(self, nl_msg):  argument
81 self.nl_msg = nl_msg
83 def __str__(self): argument
84 return f"Netlink error: {os.strerror(-self.nl_msg.error)}\n{self.nl_msg}"
100 def __init__(self, raw, offset): argument
101 self._len, self._type = struct.unpack("HH", raw[offset : offset + 4])
102 self.type = self._type & ~Netlink.NLA_TYPE_MASK
103 self.is_nest = self._type & Netlink.NLA_F_NESTED
104 self.payload_len = self._len
105 self.full_len = (self.payload_len + 3) & ~3
106 self.raw = raw[offset + 4 : offset + self.payload_len]
130 def as_scalar(self, attr_type, byte_order=None): argument
131 format = self.get_format(attr_type, byte_order)
132 return format.unpack(self.raw)[0]
134 def as_auto_scalar(self, attr_type, byte_order=None): argument
135 if len(self.raw) != 4 and len(self.raw) != 8:
136 raise Exception(f"Auto-scalar len payload be 4 or 8 bytes, got {len(self.raw)}")
137 real_type = attr_type[0] + str(len(self.raw) * 8)
138 format = self.get_format(real_type, byte_order)
139 return format.unpack(self.raw)[0]
141 def as_strz(self): argument
142 return self.raw.decode('ascii')[:-1]
144 def as_bin(self): argument
145 return self.raw
147 def as_c_array(self, type): argument
148 format = self.get_format(type)
149 return [ x[0] for x in format.iter_unpack(self.raw) ]
151 def as_struct(self, members): argument
157 decoded = self.raw[offset : offset + m['len']]
160 format = self.get_format(m.type, m.byte_order)
161 [ decoded ] = format.unpack_from(self.raw, offset)
164 decoded = self.formatted_string(decoded, m.display_hint)
168 def __repr__(self): argument
169 return f"[type:{self.type} len:{self._len}] {self.raw}"
173 def __init__(self, msg, offset=0): argument
174 self.attrs = []
179 self.attrs.append(attr)
181 def __iter__(self): argument
182 yield from self.attrs
184 def __repr__(self): argument
186 for a in self.attrs:
194 def __init__(self, msg, offset, attr_space=None): argument
195 self.hdr = msg[offset : offset + 16]
197 self.nl_len, self.nl_type, self.nl_flags, self.nl_seq, self.nl_portid = \
198 struct.unpack("IHHII", self.hdr)
200 self.raw = msg[offset + 16 : offset + self.nl_len]
202 self.error = 0
203 self.done = 0
206 if self.nl_type == Netlink.NLMSG_ERROR:
207 self.error = struct.unpack("i", self.raw[0:4])[0]
208 self.done = 1
210 elif self.nl_type == Netlink.NLMSG_DONE:
211 self.done = 1
214 self.extack = None
215 if self.nl_flags & Netlink.NLM_F_ACK_TLVS and extack_off:
216 self.extack = dict()
217 extack_attrs = NlAttrs(self.raw[extack_off:])
220 self.extack['msg'] = extack.as_strz()
222 self.extack['miss-type'] = extack.as_scalar('u32')
224 self.extack['miss-nest'] = extack.as_scalar('u32')
226 self.extack['bad-attr-offs'] = extack.as_scalar('u32')
228 if 'unknown' not in self.extack:
229 self.extack['unknown'] = []
230 self.extack['unknown'].append(extack)
234 if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
235 miss_type = self.extack['miss-type']
241 self.extack['miss-type'] = desc
243 def cmd(self): argument
244 return self.nl_type
246 def __repr__(self): argument
247 …msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl…
248 if self.error:
249 msg += '\terror: ' + str(self.error)
250 if self.extack:
251 msg += '\textack: ' + repr(self.extack)
256 def __init__(self, data, attr_space=None): argument
257 self.msgs = []
263 self.msgs.append(msg)
265 def __iter__(self): argument
266 yield from self.msgs
335 def __init__(self, nl_msg): argument
336 self.nl = nl_msg
337 self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
338 self.raw = nl_msg.raw[4:]
340 def cmd(self): argument
341 return self.genl_cmd
343 def __repr__(self): argument
344 msg = repr(self.nl)
345 msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
346 for a in self.raw_attrs:
352 def __init__(self, family_name, proto_num): argument
353 self.family_name = family_name
354 self.proto_num = proto_num
356 def _message(self, nl_type, nl_flags, seq=None): argument
362 def message(self, flags, command, version, seq=None): argument
363 return self._message(command, flags, seq)
365 def _decode(self, nl_msg): argument
368 def decode(self, ynl, nl_msg): argument
369 msg = self._decode(nl_msg)
377 def get_mcast_id(self, mcast_name, mcast_groups): argument
384 def __init__(self, family_name): argument
391 self.genl_family = genl_family_name_to_id[family_name]
392 self.family_id = genl_family_name_to_id[family_name]['id']
394 def message(self, flags, command, version, seq=None): argument
395 nlmsg = self._message(self.family_id, flags, seq)
399 def _decode(self, nl_msg): argument
402 def get_mcast_id(self, mcast_name, mcast_groups): argument
403 if mcast_name not in self.genl_family['mcast']:
405 return self.genl_family['mcast'][mcast_name]
414 def __init__(self, def_path, schema=None, process_unknown=False): argument
417 self.include_raw = False
418 self.process_unknown = process_unknown
421 if self.proto == "netlink-raw":
422 self.nlproto = NetlinkProtocol(self.yaml['name'],
423 self.yaml['protonum'])
425 self.nlproto = GenlProtocol(self.yaml['name'])
427 raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
429 self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
430 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
431 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
432 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
434 self.async_msg_ids = set()
435 self.async_msg_queue = []
437 for msg in self.msgs.values():
439 self.async_msg_ids.add(msg.rsp_value)
441 for op_name, op in self.ops.items():
442 bound_f = functools.partial(self._op, op_name)
443 setattr(self, op.ident_name, bound_f)
446 def ntf_subscribe(self, mcast_name): argument
447 mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
448 self.sock.bind((0, 0))
449 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
452 def _add_attr(self, space, name, value): argument
454 attr = self.attr_sets[space][name]
462 attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue)
490 def _decode_enum(self, raw, attr_spec): argument
491 enum = self.consts[attr_spec['enum']]
504 def _decode_binary(self, attr, attr_spec): argument
506 members = self.consts[attr_spec.struct_name]
510 decoded[m.name] = self._decode_enum(decoded[m.name], m)
519 def _decode_array_nest(self, attr, attr_spec): argument
526 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
530 def _decode_unknown(self, attr): argument
532 return self._decode(NlAttrs(attr.raw), None)
536 def _rsp_add(self, rsp, name, is_multi, decoded): argument
551 def _resolve_selector(self, attr_spec, vals): argument
553 if sub_msg not in self.sub_msgs:
555 sub_msg_spec = self.sub_msgs[sub_msg]
567 def _decode_sub_msg(self, attr, attr_spec, rsp): argument
568 msg_format = self._resolve_selector(attr_spec, rsp)
572 decoded.update(self._decode_fixed_header(attr, msg_format.fixed_header));
573 offset = self._fixed_header_size(msg_format.fixed_header)
575 if msg_format.attr_set in self.attr_sets:
576 subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
582 def _decode(self, attrs, space): argument
584 attr_space = self.attr_sets[space]
590 if not self.process_unknown:
593 self._rsp_add(rsp, attr_name, None, self._decode_unknown(attr))
597 subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'])
602 decoded = self._decode_binary(attr, attr_spec)
610 decoded = self._decode_enum(decoded, attr_spec)
612 decoded = self._decode_array_nest(attr, attr_spec)
616 value = self._decode_enum(value, attr_spec)
617 selector = self._decode_enum(selector, attr_spec)
620 decoded = self._decode_sub_msg(attr, attr_spec, rsp)
622 if not self.process_unknown:
624 decoded = self._decode_unknown(attr)
626 self._rsp_add(rsp, attr_spec["name"], attr_spec.is_multi, decoded)
630 def _decode_extack_path(self, attrs, attr_set, offset, target): argument
647 subpath = self._decode_extack_path(NlAttrs(attr.raw),
648 self.attr_sets[attr_spec['nested-attributes']],
656 def _decode_extack(self, request, op, extack): argument
660 msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set))
661 offset = 20 + self._fixed_header_size(op.fixed_header)
662 path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
668 def _fixed_header_size(self, name): argument
670 fixed_header_members = self.consts[name].members
682 def _decode_fixed_header(self, msg, name): argument
683 fixed_header_members = self.consts[name].members
699 value = self._decode_enum(value, m)
703 def handle_ntf(self, decoded): argument
705 if self.include_raw:
707 op = self.rsp_by_value[decoded.cmd()]
708 attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
710 attrs.update(self._decode_fixed_header(decoded, op.fixed_header))
714 self.async_msg_queue.append(msg)
716 def check_ntf(self): argument
719 reply = self.sock.recv(128 * 1024, socket.MSG_DONTWAIT)
733 decoded = self.nlproto.decode(self, nl_msg)
734 if decoded.cmd() not in self.async_msg_ids:
738 self.handle_ntf(decoded)
740 def operation_do_attributes(self, name): argument
745 op = self.find_operation(name)
751 def _op(self, method, vals, flags=None, dump=False): argument
752 op = self.ops[method]
761 msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
764 fixed_header_members = self.consts[op.fixed_header].members
775 msg += self._add_attr(op.attr_set.name, name, value)
778 self.sock.send(msg, 0)
783 reply = self.sock.recv(128 * 1024)
787 self._decode_extack(msg, op, nl_msg.extack)
798 decoded = self.nlproto.decode(self, nl_msg)
802 if decoded.cmd() in self.async_msg_ids:
803 self.handle_ntf(decoded)
809 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
811 rsp_msg.update(self._decode_fixed_header(decoded, op.fixed_header))
820 def do(self, method, vals, flags=None): argument
821 return self._op(method, vals, flags)
823 def dump(self, method, vals): argument
824 return self._op(method, vals, [], dump=True)