Lines Matching full:self
104 def __init__(self, nl_msg): argument
105 self.nl_msg = nl_msg
106 self.error = -nl_msg.error
108 def __str__(self): argument
109 return f"Netlink error: {os.strerror(self.error)}\n{self.nl_msg}"
129 def __init__(self, raw, offset): argument
130 self._len, self._type = struct.unpack("HH", raw[offset : offset + 4])
131 self.type = self._type & ~Netlink.NLA_TYPE_MASK
132 self.is_nest = self._type & Netlink.NLA_F_NESTED
133 self.payload_len = self._len
134 self.full_len = (self.payload_len + 3) & ~3
135 self.raw = raw[offset + 4 : offset + self.payload_len]
145 def as_scalar(self, attr_type, byte_order=None): argument
146 format = self.get_format(attr_type, byte_order)
147 return format.unpack(self.raw)[0]
149 def as_auto_scalar(self, attr_type, byte_order=None): argument
150 if len(self.raw) != 4 and len(self.raw) != 8:
151 raise Exception(f"Auto-scalar len payload be 4 or 8 bytes, got {len(self.raw)}")
152 real_type = attr_type[0] + str(len(self.raw) * 8)
153 format = self.get_format(real_type, byte_order)
154 return format.unpack(self.raw)[0]
156 def as_strz(self): argument
157 return self.raw.decode('ascii')[:-1]
159 def as_bin(self): argument
160 return self.raw
162 def as_c_array(self, type): argument
163 format = self.get_format(type)
164 return [ x[0] for x in format.iter_unpack(self.raw) ]
166 def __repr__(self): argument
167 return f"[type:{self.type} len:{self._len}] {self.raw}"
171 def __init__(self, msg, offset=0): argument
172 self.attrs = []
177 self.attrs.append(attr)
179 def __iter__(self): argument
180 yield from self.attrs
182 def __repr__(self): argument
184 for a in self.attrs:
192 def __init__(self, msg, offset, attr_space=None): argument
193 self.hdr = msg[offset : offset + 16]
195 self.nl_len, self.nl_type, self.nl_flags, self.nl_seq, self.nl_portid = \
196 struct.unpack("IHHII", self.hdr)
198 self.raw = msg[offset + 16 : offset + self.nl_len]
200 self.error = 0
201 self.done = 0
204 if self.nl_type == Netlink.NLMSG_ERROR:
205 self.error = struct.unpack("i", self.raw[0:4])[0]
206 self.done = 1
208 elif self.nl_type == Netlink.NLMSG_DONE:
209 self.error = struct.unpack("i", self.raw[0:4])[0]
210 self.done = 1
213 self.extack = None
214 if self.nl_flags & Netlink.NLM_F_ACK_TLVS and extack_off:
215 self.extack = dict()
216 extack_attrs = NlAttrs(self.raw[extack_off:])
219 self.extack['msg'] = extack.as_strz()
221 self.extack['miss-type'] = extack.as_scalar('u32')
223 self.extack['miss-nest'] = extack.as_scalar('u32')
225 self.extack['bad-attr-offs'] = extack.as_scalar('u32')
227 self.extack['policy'] = self._decode_policy(extack.raw)
229 if 'unknown' not in self.extack:
230 self.extack['unknown'] = []
231 self.extack['unknown'].append(extack)
235 if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
236 miss_type = self.extack['miss-type']
239 self.extack['miss-type'] = spec['name']
241 self.extack['miss-type-doc'] = spec['doc']
243 def _decode_policy(self, raw): argument
267 def cmd(self): argument
268 return self.nl_type
270 def __repr__(self): argument
271 …msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl…
272 if self.error:
273 msg += '\n\terror: ' + str(self.error)
274 if self.extack:
275 msg += '\n\textack: ' + repr(self.extack)
280 def __init__(self, data, attr_space=None): argument
281 self.msgs = []
287 self.msgs.append(msg)
289 def __iter__(self): argument
290 yield from self.msgs
359 def __init__(self, nl_msg): argument
360 self.nl = nl_msg
361 self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
362 self.raw = nl_msg.raw[4:]
364 def cmd(self): argument
365 return self.genl_cmd
367 def __repr__(self): argument
368 msg = repr(self.nl)
369 msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
370 for a in self.raw_attrs:
376 def __init__(self, family_name, proto_num): argument
377 self.family_name = family_name
378 self.proto_num = proto_num
380 def _message(self, nl_type, nl_flags, seq=None): argument
386 def message(self, flags, command, version, seq=None): argument
387 return self._message(command, flags, seq)
389 def _decode(self, nl_msg): argument
392 def decode(self, ynl, nl_msg, op): argument
393 msg = self._decode(nl_msg)
400 def get_mcast_id(self, mcast_name, mcast_groups): argument
405 def msghdr_size(self): argument
410 def __init__(self, family_name): argument
417 self.genl_family = genl_family_name_to_id[family_name]
418 self.family_id = genl_family_name_to_id[family_name]['id']
420 def message(self, flags, command, version, seq=None): argument
421 nlmsg = self._message(self.family_id, flags, seq)
425 def _decode(self, nl_msg): argument
428 def get_mcast_id(self, mcast_name, mcast_groups): argument
429 if mcast_name not in self.genl_family['mcast']:
431 return self.genl_family['mcast'][mcast_name]
433 def msghdr_size(self): argument
440 def __init__(self, attr_space, attrs, outer = None): argument
442 inner_scope = self.SpecValuesPair(attr_space, attrs)
443 self.scopes = [inner_scope] + outer_scopes
445 def lookup(self, name): argument
446 for scope in self.scopes:
462 def __init__(self, def_path, schema=None, process_unknown=False, argument
466 self.include_raw = False
467 self.process_unknown = process_unknown
470 if self.proto == "netlink-raw":
471 self.nlproto = NetlinkProtocol(self.yaml['name'],
472 self.yaml['protonum'])
474 self.nlproto = GenlProtocol(self.yaml['name'])
476 raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
478 self._recv_dbg = False
482 self._recv_size = recv_size if recv_size else 131072
486 if self._recv_size < 4000:
489 self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
490 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
491 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
492 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
494 self.async_msg_ids = set()
495 self.async_msg_queue = queue.Queue()
497 for msg in self.msgs.values():
499 self.async_msg_ids.add(msg.rsp_value)
501 for op_name, op in self.ops.items():
502 bound_f = functools.partial(self._op, op_name)
503 setattr(self, op.ident_name, bound_f)
506 def ntf_subscribe(self, mcast_name): argument
507 mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
508 self.sock.bind((0, 0))
509 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
512 def set_recv_dbg(self, enabled): argument
513 self._recv_dbg = enabled
515 def _recv_dbg_print(self, reply, nl_msgs): argument
516 if not self._recv_dbg:
523 def _encode_enum(self, attr_spec, value): argument
524 enum = self.consts[attr_spec['enum']]
535 def _get_scalar(self, attr_spec, value): argument
541 return self._encode_enum(attr_spec, value)
543 def _add_attr(self, space, name, value, search_attrs): argument
545 attr = self.attr_sets[space][name]
553 attr_payload += self._add_attr(space, name, subvalue, search_attrs)
560 sub_attrs = SpaceAttrs(self.attr_sets[sub_space], value, search_attrs)
562 attr_payload += self._add_attr(sub_space, subname, subvalue, sub_attrs)
576 attr_payload = self._encode_struct(attr.struct_name, value)
580 scalar = self._get_scalar(attr, value)
588 scalar_value = self._get_scalar(attr, value["value"])
589 scalar_selector = self._get_scalar(attr, value["selector"])
592 msg_format = self._resolve_selector(attr, search_attrs)
595 attr_payload += self._encode_struct(msg_format.fixed_header, value)
597 if msg_format.attr_set in self.attr_sets:
601 attr_payload += self._add_attr(msg_format.attr_set,
611 def _decode_enum(self, raw, attr_spec): argument
612 enum = self.consts[attr_spec['enum']]
625 def _decode_binary(self, attr, attr_spec): argument
627 decoded = self._decode_struct(attr.raw, attr_spec.struct_name)
633 decoded = self._formatted_string(decoded, attr_spec.display_hint)
636 def _decode_array_attr(self, attr, attr_spec): argument
644 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
649 subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
654 subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
660 def _decode_nest_type_value(self, attr, attr_spec): argument
666 subattrs = self._decode(NlAttrs(value.raw), attr_spec['nested-attributes'])
670 def _decode_unknown(self, attr): argument
672 return self._decode(NlAttrs(attr.raw), None)
676 def _rsp_add(self, rsp, name, is_multi, decoded): argument
691 def _resolve_selector(self, attr_spec, search_attrs): argument
693 if sub_msg not in self.sub_msgs:
695 sub_msg_spec = self.sub_msgs[sub_msg]
705 def _decode_sub_msg(self, attr, attr_spec, search_attrs): argument
706 msg_format = self._resolve_selector(attr_spec, search_attrs)
710 decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
711 offset = self._struct_size(msg_format.fixed_header)
713 if msg_format.attr_set in self.attr_sets:
714 subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
720 def _decode(self, attrs, space, outer_attrs = None): argument
723 attr_space = self.attr_sets[space]
730 if not self.process_unknown:
733 self._rsp_add(rsp, attr_name, None, self._decode_unknown(attr))
738 … subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'], search_attrs)
743 decoded = self._decode_binary(attr, attr_spec)
751 decoded = self._decode_enum(decoded, attr_spec)
753 decoded = self._formatted_string(decoded, attr_spec.display_hint)
755 decoded = self._decode_array_attr(attr, attr_spec)
759 value = self._decode_enum(value, attr_spec)
760 selector = self._decode_enum(selector, attr_spec)
763 decoded = self._decode_sub_msg(attr, attr_spec, search_attrs)
765 decoded = self._decode_nest_type_value(attr, attr_spec)
767 if not self.process_unknown:
769 decoded = self._decode_unknown(attr)
771 self._rsp_add(rsp, attr_spec["name"], attr_spec.is_multi, decoded)
778 def _decode_extack_path(self, attrs, attr_set, offset, target): argument
795 subpath = self._decode_extack_path(NlAttrs(attr.raw),
796 self.attr_sets[attr_spec['nested-attributes']],
804 def _decode_extack(self, request, op, extack): argument
808 msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set), op)
809 offset = self.nlproto.msghdr_size() + self._struct_size(op.fixed_header)
810 path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
816 def _struct_size(self, name): argument
818 members = self.consts[name].members
823 size += self._struct_size(m.struct)
833 def _decode_struct(self, data, name): argument
834 members = self.consts[name].members
843 len = self._struct_size(m.struct)
844 value = self._decode_struct(data[offset : offset + len],
856 value = self._decode_enum(value, m)
858 value = self._formatted_string(value, m.display_hint)
862 def _encode_struct(self, name, vals): argument
863 members = self.consts[name].members
873 attr_payload += self._encode_struct(m.struct, value)
886 def _formatted_string(self, raw, display_hint): argument
902 def handle_ntf(self, decoded): argument
904 if self.include_raw:
906 op = self.rsp_by_value[decoded.cmd()]
907 attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
909 attrs.update(self._decode_struct(decoded.raw, op.fixed_header))
913 self.async_msg_queue.put(msg)
915 def check_ntf(self): argument
918 reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
923 self._recv_dbg_print(reply, nms)
933 decoded = self.nlproto.decode(self, nl_msg, None)
934 if decoded.cmd() not in self.async_msg_ids:
938 self.handle_ntf(decoded)
940 def poll_ntf(self, duration=None): argument
943 selector.register(self.sock, selectors.EVENT_READ)
947 yield self.async_msg_queue.get_nowait()
957 self.check_ntf()
959 def operation_do_attributes(self, name): argument
964 op = self.find_operation(name)
970 def _encode_message(self, op, vals, flags, req_seq): argument
975 msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
977 msg += self._encode_struct(op.fixed_header, vals)
980 msg += self._add_attr(op.attr_set.name, name, value, search_attrs)
984 def _ops(self, ops): argument
989 op = self.ops[method]
990 msg = self._encode_message(op, vals, flags, req_seq)
995 self.sock.send(payload, 0)
1001 reply = self.sock.recv(self._recv_size)
1003 self._recv_dbg_print(reply, nms)
1008 self._decode_extack(req_msg, op, nl_msg.extack)
1034 decoded = self.nlproto.decode(self, nl_msg, op)
1038 if decoded.cmd() in self.async_msg_ids:
1039 self.handle_ntf(decoded)
1045 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
1047 rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
1052 def _op(self, method, vals, flags=None, dump=False): argument
1058 return self._ops(ops)[0]
1060 def do(self, method, vals, flags=None): argument
1061 return self._op(method, vals, flags)
1063 def dump(self, method, vals): argument
1064 return self._op(method, vals, dump=True)
1066 def do_multi(self, ops): argument
1067 return self._ops(ops)