Lines Matching full:self

101   def __init__(self, nl_msg):  argument
102 self.nl_msg = nl_msg
103 self.error = -nl_msg.error
105 def __str__(self): argument
106 return f"Netlink error: {os.strerror(self.error)}\n{self.nl_msg}"
126 def __init__(self, raw, offset): argument
127 self._len, self._type = struct.unpack("HH", raw[offset : offset + 4])
128 self.type = self._type & ~Netlink.NLA_TYPE_MASK
129 self.is_nest = self._type & Netlink.NLA_F_NESTED
130 self.payload_len = self._len
131 self.full_len = (self.payload_len + 3) & ~3
132 self.raw = raw[offset + 4 : offset + self.payload_len]
142 def as_scalar(self, attr_type, byte_order=None): argument
143 format = self.get_format(attr_type, byte_order)
144 return format.unpack(self.raw)[0]
146 def as_auto_scalar(self, attr_type, byte_order=None): argument
147 if len(self.raw) != 4 and len(self.raw) != 8:
148 raise Exception(f"Auto-scalar len payload be 4 or 8 bytes, got {len(self.raw)}")
149 real_type = attr_type[0] + str(len(self.raw) * 8)
150 format = self.get_format(real_type, byte_order)
151 return format.unpack(self.raw)[0]
153 def as_strz(self): argument
154 return self.raw.decode('ascii')[:-1]
156 def as_bin(self): argument
157 return self.raw
159 def as_c_array(self, type): argument
160 format = self.get_format(type)
161 return [ x[0] for x in format.iter_unpack(self.raw) ]
163 def __repr__(self): argument
164 return f"[type:{self.type} len:{self._len}] {self.raw}"
168 def __init__(self, msg, offset=0): argument
169 self.attrs = []
174 self.attrs.append(attr)
176 def __iter__(self): argument
177 yield from self.attrs
179 def __repr__(self): argument
181 for a in self.attrs:
189 def __init__(self, msg, offset, attr_space=None): argument
190 self.hdr = msg[offset : offset + 16]
192 self.nl_len, self.nl_type, self.nl_flags, self.nl_seq, self.nl_portid = \
193 struct.unpack("IHHII", self.hdr)
195 self.raw = msg[offset + 16 : offset + self.nl_len]
197 self.error = 0
198 self.done = 0
201 if self.nl_type == Netlink.NLMSG_ERROR:
202 self.error = struct.unpack("i", self.raw[0:4])[0]
203 self.done = 1
205 elif self.nl_type == Netlink.NLMSG_DONE:
206 self.error = struct.unpack("i", self.raw[0:4])[0]
207 self.done = 1
210 self.extack = None
211 if self.nl_flags & Netlink.NLM_F_ACK_TLVS and extack_off:
212 self.extack = dict()
213 extack_attrs = NlAttrs(self.raw[extack_off:])
216 self.extack['msg'] = extack.as_strz()
218 self.extack['miss-type'] = extack.as_scalar('u32')
220 self.extack['miss-nest'] = extack.as_scalar('u32')
222 self.extack['bad-attr-offs'] = extack.as_scalar('u32')
224 self.extack['policy'] = self._decode_policy(extack.raw)
226 if 'unknown' not in self.extack:
227 self.extack['unknown'] = []
228 self.extack['unknown'].append(extack)
232 if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
233 miss_type = self.extack['miss-type']
236 self.extack['miss-type'] = spec['name']
238 self.extack['miss-type-doc'] = spec['doc']
240 def _decode_policy(self, raw): argument
264 def cmd(self): argument
265 return self.nl_type
267 def __repr__(self): argument
268 …msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl…
269 if self.error:
270 msg += '\n\terror: ' + str(self.error)
271 if self.extack:
272 msg += '\n\textack: ' + repr(self.extack)
277 def __init__(self, data, attr_space=None): argument
278 self.msgs = []
284 self.msgs.append(msg)
286 def __iter__(self): argument
287 yield from self.msgs
356 def __init__(self, nl_msg): argument
357 self.nl = nl_msg
358 self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
359 self.raw = nl_msg.raw[4:]
361 def cmd(self): argument
362 return self.genl_cmd
364 def __repr__(self): argument
365 msg = repr(self.nl)
366 msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
367 for a in self.raw_attrs:
373 def __init__(self, family_name, proto_num): argument
374 self.family_name = family_name
375 self.proto_num = proto_num
377 def _message(self, nl_type, nl_flags, seq=None): argument
383 def message(self, flags, command, version, seq=None): argument
384 return self._message(command, flags, seq)
386 def _decode(self, nl_msg): argument
389 def decode(self, ynl, nl_msg, op): argument
390 msg = self._decode(nl_msg)
397 def get_mcast_id(self, mcast_name, mcast_groups): argument
402 def msghdr_size(self): argument
407 def __init__(self, family_name): argument
414 self.genl_family = genl_family_name_to_id[family_name]
415 self.family_id = genl_family_name_to_id[family_name]['id']
417 def message(self, flags, command, version, seq=None): argument
418 nlmsg = self._message(self.family_id, flags, seq)
422 def _decode(self, nl_msg): argument
425 def get_mcast_id(self, mcast_name, mcast_groups): argument
426 if mcast_name not in self.genl_family['mcast']:
428 return self.genl_family['mcast'][mcast_name]
430 def msghdr_size(self): argument
437 def __init__(self, attr_space, attrs, outer = None): argument
439 inner_scope = self.SpecValuesPair(attr_space, attrs)
440 self.scopes = [inner_scope] + outer_scopes
442 def lookup(self, name): argument
443 for scope in self.scopes:
459 def __init__(self, def_path, schema=None, process_unknown=False, argument
463 self.include_raw = False
464 self.process_unknown = process_unknown
467 if self.proto == "netlink-raw":
468 self.nlproto = NetlinkProtocol(self.yaml['name'],
469 self.yaml['protonum'])
471 self.nlproto = GenlProtocol(self.yaml['name'])
473 raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
475 self._recv_dbg = False
479 self._recv_size = recv_size if recv_size else 131072
483 if self._recv_size < 4000:
486 self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
487 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
488 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
489 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
491 self.async_msg_ids = set()
492 self.async_msg_queue = []
494 for msg in self.msgs.values():
496 self.async_msg_ids.add(msg.rsp_value)
498 for op_name, op in self.ops.items():
499 bound_f = functools.partial(self._op, op_name)
500 setattr(self, op.ident_name, bound_f)
503 def ntf_subscribe(self, mcast_name): argument
504 mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
505 self.sock.bind((0, 0))
506 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
509 def set_recv_dbg(self, enabled): argument
510 self._recv_dbg = enabled
512 def _recv_dbg_print(self, reply, nl_msgs): argument
513 if not self._recv_dbg:
520 def _encode_enum(self, attr_spec, value): argument
521 enum = self.consts[attr_spec['enum']]
532 def _get_scalar(self, attr_spec, value): argument
538 return self._encode_enum(attr_spec, value)
540 def _add_attr(self, space, name, value, search_attrs): argument
542 attr = self.attr_sets[space][name]
550 attr_payload += self._add_attr(space, name, subvalue, search_attrs)
556 sub_attrs = SpaceAttrs(self.attr_sets[space], value, search_attrs)
558 attr_payload += self._add_attr(attr['nested-attributes'],
573 attr_payload = self._encode_struct(attr.struct_name, value)
577 scalar = self._get_scalar(attr, value)
585 scalar_value = self._get_scalar(attr, value["value"])
586 scalar_selector = self._get_scalar(attr, value["selector"])
589 msg_format = self._resolve_selector(attr, search_attrs)
592 attr_payload += self._encode_struct(msg_format.fixed_header, value)
594 if msg_format.attr_set in self.attr_sets:
598 attr_payload += self._add_attr(msg_format.attr_set,
608 def _decode_enum(self, raw, attr_spec): argument
609 enum = self.consts[attr_spec['enum']]
622 def _decode_binary(self, attr, attr_spec): argument
624 decoded = self._decode_struct(attr.raw, attr_spec.struct_name)
630 decoded = self._formatted_string(decoded, attr_spec.display_hint)
633 def _decode_array_attr(self, attr, attr_spec): argument
641 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
646 subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
651 subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
657 def _decode_nest_type_value(self, attr, attr_spec): argument
663 subattrs = self._decode(NlAttrs(value.raw), attr_spec['nested-attributes'])
667 def _decode_unknown(self, attr): argument
669 return self._decode(NlAttrs(attr.raw), None)
673 def _rsp_add(self, rsp, name, is_multi, decoded): argument
688 def _resolve_selector(self, attr_spec, search_attrs): argument
690 if sub_msg not in self.sub_msgs:
692 sub_msg_spec = self.sub_msgs[sub_msg]
702 def _decode_sub_msg(self, attr, attr_spec, search_attrs): argument
703 msg_format = self._resolve_selector(attr_spec, search_attrs)
707 decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
708 offset = self._struct_size(msg_format.fixed_header)
710 if msg_format.attr_set in self.attr_sets:
711 subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
717 def _decode(self, attrs, space, outer_attrs = None): argument
720 attr_space = self.attr_sets[space]
727 if not self.process_unknown:
730 self._rsp_add(rsp, attr_name, None, self._decode_unknown(attr))
734 … subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'], search_attrs)
739 decoded = self._decode_binary(attr, attr_spec)
747 decoded = self._decode_enum(decoded, attr_spec)
749 decoded = self._formatted_string(decoded, attr_spec.display_hint)
751 decoded = self._decode_array_attr(attr, attr_spec)
755 value = self._decode_enum(value, attr_spec)
756 selector = self._decode_enum(selector, attr_spec)
759 decoded = self._decode_sub_msg(attr, attr_spec, search_attrs)
761 decoded = self._decode_nest_type_value(attr, attr_spec)
763 if not self.process_unknown:
765 decoded = self._decode_unknown(attr)
767 self._rsp_add(rsp, attr_spec["name"], attr_spec.is_multi, decoded)
771 def _decode_extack_path(self, attrs, attr_set, offset, target): argument
788 subpath = self._decode_extack_path(NlAttrs(attr.raw),
789 self.attr_sets[attr_spec['nested-attributes']],
797 def _decode_extack(self, request, op, extack): argument
801 msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set), op)
802 offset = self.nlproto.msghdr_size() + self._struct_size(op.fixed_header)
803 path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
809 def _struct_size(self, name): argument
811 members = self.consts[name].members
816 size += self._struct_size(m.struct)
826 def _decode_struct(self, data, name): argument
827 members = self.consts[name].members
836 len = self._struct_size(m.struct)
837 value = self._decode_struct(data[offset : offset + len],
849 value = self._decode_enum(value, m)
851 value = self._formatted_string(value, m.display_hint)
855 def _encode_struct(self, name, vals): argument
856 members = self.consts[name].members
866 attr_payload += self._encode_struct(m.struct, value)
879 def _formatted_string(self, raw, display_hint): argument
895 def handle_ntf(self, decoded): argument
897 if self.include_raw:
899 op = self.rsp_by_value[decoded.cmd()]
900 attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
902 attrs.update(self._decode_struct(decoded.raw, op.fixed_header))
906 self.async_msg_queue.append(msg)
908 def check_ntf(self): argument
911 reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
916 self._recv_dbg_print(reply, nms)
926 decoded = self.nlproto.decode(self, nl_msg, None)
927 if decoded.cmd() not in self.async_msg_ids:
931 self.handle_ntf(decoded)
933 def operation_do_attributes(self, name): argument
938 op = self.find_operation(name)
944 def _encode_message(self, op, vals, flags, req_seq): argument
949 msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
951 msg += self._encode_struct(op.fixed_header, vals)
954 msg += self._add_attr(op.attr_set.name, name, value, search_attrs)
958 def _ops(self, ops): argument
963 op = self.ops[method]
964 msg = self._encode_message(op, vals, flags, req_seq)
969 self.sock.send(payload, 0)
975 reply = self.sock.recv(self._recv_size)
977 self._recv_dbg_print(reply, nms)
982 self._decode_extack(req_msg, op, nl_msg.extack)
1008 decoded = self.nlproto.decode(self, nl_msg, op)
1012 if decoded.cmd() in self.async_msg_ids:
1013 self.handle_ntf(decoded)
1019 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
1021 rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
1026 def _op(self, method, vals, flags=None, dump=False): argument
1032 return self._ops(ops)[0]
1034 def do(self, method, vals, flags=None): argument
1035 return self._op(method, vals, flags)
1037 def dump(self, method, vals): argument
1038 return self._op(method, vals, dump=True)
1040 def do_multi(self, ops): argument
1041 return self._ops(ops)