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)
234 self.annotate_extack(attr_space)
236 def _decode_policy(self, raw): argument
260 def annotate_extack(self, attr_space): argument
264 if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
265 miss_type = self.extack['miss-type']
268 self.extack['miss-type'] = spec['name']
270 self.extack['miss-type-doc'] = spec['doc']
272 def cmd(self): argument
273 return self.nl_type
275 def __repr__(self): argument
276 …msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl…
277 if self.error:
278 msg += '\n\terror: ' + str(self.error)
279 if self.extack:
280 msg += '\n\textack: ' + repr(self.extack)
285 def __init__(self, data): argument
286 self.msgs = []
292 self.msgs.append(msg)
294 def __iter__(self): argument
295 yield from self.msgs
364 def __init__(self, nl_msg): argument
365 self.nl = nl_msg
366 self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
367 self.raw = nl_msg.raw[4:]
369 def cmd(self): argument
370 return self.genl_cmd
372 def __repr__(self): argument
373 msg = repr(self.nl)
374 msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
375 for a in self.raw_attrs:
381 def __init__(self, family_name, proto_num): argument
382 self.family_name = family_name
383 self.proto_num = proto_num
385 def _message(self, nl_type, nl_flags, seq=None): argument
391 def message(self, flags, command, version, seq=None): argument
392 return self._message(command, flags, seq)
394 def _decode(self, nl_msg): argument
397 def decode(self, ynl, nl_msg, op): argument
398 msg = self._decode(nl_msg)
405 def get_mcast_id(self, mcast_name, mcast_groups): argument
410 def msghdr_size(self): argument
415 def __init__(self, family_name): argument
422 self.genl_family = genl_family_name_to_id[family_name]
423 self.family_id = genl_family_name_to_id[family_name]['id']
425 def message(self, flags, command, version, seq=None): argument
426 nlmsg = self._message(self.family_id, flags, seq)
430 def _decode(self, nl_msg): argument
433 def get_mcast_id(self, mcast_name, mcast_groups): argument
434 if mcast_name not in self.genl_family['mcast']:
436 return self.genl_family['mcast'][mcast_name]
438 def msghdr_size(self): argument
445 def __init__(self, attr_space, attrs, outer = None): argument
447 inner_scope = self.SpecValuesPair(attr_space, attrs)
448 self.scopes = [inner_scope] + outer_scopes
450 def lookup(self, name): argument
451 for scope in self.scopes:
467 def __init__(self, def_path, schema=None, process_unknown=False, argument
471 self.include_raw = False
472 self.process_unknown = process_unknown
475 if self.proto == "netlink-raw":
476 self.nlproto = NetlinkProtocol(self.yaml['name'],
477 self.yaml['protonum'])
479 self.nlproto = GenlProtocol(self.yaml['name'])
481 raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
483 self._recv_dbg = False
487 self._recv_size = recv_size if recv_size else 131072
491 if self._recv_size < 4000:
494 self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
495 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
496 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
497 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
499 self.async_msg_ids = set()
500 self.async_msg_queue = queue.Queue()
502 for msg in self.msgs.values():
504 self.async_msg_ids.add(msg.rsp_value)
506 for op_name, op in self.ops.items():
507 bound_f = functools.partial(self._op, op_name)
508 setattr(self, op.ident_name, bound_f)
511 def ntf_subscribe(self, mcast_name): argument
512 mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
513 self.sock.bind((0, 0))
514 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
517 def set_recv_dbg(self, enabled): argument
518 self._recv_dbg = enabled
520 def _recv_dbg_print(self, reply, nl_msgs): argument
521 if not self._recv_dbg:
528 def _encode_enum(self, attr_spec, value): argument
529 enum = self.consts[attr_spec['enum']]
540 def _get_scalar(self, attr_spec, value): argument
545 return self._encode_enum(attr_spec, value)
547 return self._from_string(value, attr_spec)
550 def _add_attr(self, space, name, value, search_attrs): argument
552 attr = self.attr_sets[space][name]
560 attr_payload += self._add_attr(space, name, subvalue, search_attrs)
567 sub_attrs = SpaceAttrs(self.attr_sets[sub_space], value, search_attrs)
569 attr_payload += self._add_attr(sub_space, subname, subvalue, sub_attrs)
582 attr_payload = self._from_string(value, attr)
586 attr_payload = self._encode_struct(attr.struct_name, value)
590 scalar = self._get_scalar(attr, value)
598 scalar_value = self._get_scalar(attr, value["value"])
599 scalar_selector = self._get_scalar(attr, value["selector"])
602 msg_format, _ = self._resolve_selector(attr, search_attrs)
605 attr_payload += self._encode_struct(msg_format.fixed_header, value)
607 if msg_format.attr_set in self.attr_sets:
611 attr_payload += self._add_attr(msg_format.attr_set,
621 def _decode_enum(self, raw, attr_spec): argument
622 enum = self.consts[attr_spec['enum']]
635 def _decode_binary(self, attr, attr_spec): argument
637 decoded = self._decode_struct(attr.raw, attr_spec.struct_name)
641 decoded = [ self._decode_enum(x, attr_spec) for x in decoded ]
643 decoded = [ self._formatted_string(x, attr_spec.display_hint)
648 decoded = self._formatted_string(decoded, attr_spec.display_hint)
651 def _decode_array_attr(self, attr, attr_spec): argument
659 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
664 subattr = self._formatted_string(subattr, attr_spec.display_hint)
669 subattr = self._decode_enum(subattr, attr_spec)
671 subattr = self._formatted_string(subattr, attr_spec.display_hint)
677 def _decode_nest_type_value(self, attr, attr_spec): argument
683 subattrs = self._decode(NlAttrs(value.raw), attr_spec['nested-attributes'])
687 def _decode_unknown(self, attr): argument
689 return self._decode(NlAttrs(attr.raw), None)
693 def _rsp_add(self, rsp, name, is_multi, decoded): argument
708 def _resolve_selector(self, attr_spec, search_attrs): argument
710 if sub_msg not in self.sub_msgs:
712 sub_msg_spec = self.sub_msgs[sub_msg]
722 def _decode_sub_msg(self, attr, attr_spec, search_attrs): argument
723 msg_format, _ = self._resolve_selector(attr_spec, search_attrs)
727 decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
728 offset = self._struct_size(msg_format.fixed_header)
730 if msg_format.attr_set in self.attr_sets:
731 subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
737 def _decode(self, attrs, space, outer_attrs = None): argument
740 attr_space = self.attr_sets[space]
747 if not self.process_unknown:
750 self._rsp_add(rsp, attr_name, None, self._decode_unknown(attr))
755 … subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'], search_attrs)
760 decoded = self._decode_binary(attr, attr_spec)
768 decoded = self._decode_enum(decoded, attr_spec)
770 decoded = self._formatted_string(decoded, attr_spec.display_hint)
772 decoded = self._decode_array_attr(attr, attr_spec)
776 value = self._decode_enum(value, attr_spec)
777 selector = self._decode_enum(selector, attr_spec)
780 decoded = self._decode_sub_msg(attr, attr_spec, search_attrs)
782 decoded = self._decode_nest_type_value(attr, attr_spec)
784 if not self.process_unknown:
786 decoded = self._decode_unknown(attr)
788 self._rsp_add(rsp, attr_spec["name"], attr_spec.is_multi, decoded)
795 def _decode_extack_path(self, attrs, attr_set, offset, target, search_attrs): argument
812 sub_attrs = self.attr_sets[attr_spec['nested-attributes']]
815 msg_format, value = self._resolve_selector(attr_spec, search_attrs)
818 sub_attrs = self.attr_sets[msg_format.attr_set]
823 subpath = self._decode_extack_path(NlAttrs(attr.raw), sub_attrs,
831 def _decode_extack(self, request, op, extack, vals): argument
835 msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set), op)
836 offset = self.nlproto.msghdr_size() + self._struct_size(op.fixed_header)
838 path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
844 def _struct_size(self, name): argument
846 members = self.consts[name].members
851 size += self._struct_size(m.struct)
861 def _decode_struct(self, data, name): argument
862 members = self.consts[name].members
871 len = self._struct_size(m.struct)
872 value = self._decode_struct(data[offset : offset + len],
884 value = self._decode_enum(value, m)
886 value = self._formatted_string(value, m.display_hint)
890 def _encode_struct(self, name, vals): argument
891 members = self.consts[name].members
901 attr_payload += self._encode_struct(m.struct, value)
914 def _formatted_string(self, raw, display_hint): argument
930 def _from_string(self, string, attr_spec): argument
942 def handle_ntf(self, decoded): argument
944 if self.include_raw:
946 op = self.rsp_by_value[decoded.cmd()]
947 attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
949 attrs.update(self._decode_struct(decoded.raw, op.fixed_header))
953 self.async_msg_queue.put(msg)
955 def check_ntf(self): argument
958 reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
963 self._recv_dbg_print(reply, nms)
973 decoded = self.nlproto.decode(self, nl_msg, None)
974 if decoded.cmd() not in self.async_msg_ids:
978 self.handle_ntf(decoded)
980 def poll_ntf(self, duration=None): argument
983 selector.register(self.sock, selectors.EVENT_READ)
987 yield self.async_msg_queue.get_nowait()
997 self.check_ntf()
999 def operation_do_attributes(self, name): argument
1004 op = self.find_operation(name)
1010 def _encode_message(self, op, vals, flags, req_seq): argument
1015 msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
1017 msg += self._encode_struct(op.fixed_header, vals)
1020 msg += self._add_attr(op.attr_set.name, name, value, search_attrs)
1024 def _ops(self, ops): argument
1029 op = self.ops[method]
1030 msg = self._encode_message(op, vals, flags, req_seq)
1035 self.sock.send(payload, 0)
1041 reply = self.sock.recv(self._recv_size)
1043 self._recv_dbg_print(reply, nms)
1049 self._decode_extack(req_msg, op, nl_msg.extack, vals)
1075 decoded = self.nlproto.decode(self, nl_msg, op)
1079 if decoded.cmd() in self.async_msg_ids:
1080 self.handle_ntf(decoded)
1086 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
1088 rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
1093 def _op(self, method, vals, flags=None, dump=False): argument
1099 return self._ops(ops)[0]
1101 def do(self, method, vals, flags=None): argument
1102 return self._op(method, vals, flags)
1104 def dump(self, method, vals): argument
1105 return self._op(method, vals, dump=True)
1107 def do_multi(self, ops): argument
1108 return self._ops(ops)