Lines Matching +full:self +full:-
1 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
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.payload_len = self._len
104 self.full_len = (self.payload_len + 3) & ~3
105 self.raw = raw[offset + 4:offset + self.payload_len]
111 return format.big if byte_order == "big-endian" \
129 def as_scalar(self, attr_type, byte_order=None): argument
130 format = self.get_format(attr_type, byte_order)
131 return format.unpack(self.raw)[0]
133 def as_strz(self): argument
134 return self.raw.decode('ascii')[:-1]
136 def as_bin(self): argument
137 return self.raw
139 def as_c_array(self, type): argument
140 format = self.get_format(type)
141 return [ x[0] for x in format.iter_unpack(self.raw) ]
143 def as_struct(self, members): argument
147 # TODO: handle non-scalar members
149 decoded = self.raw[offset:offset+m['len']]
152 format = self.get_format(m.type, m.byte_order)
153 [ decoded ] = format.unpack_from(self.raw, offset)
156 decoded = self.formatted_string(decoded, m.display_hint)
160 def __repr__(self): argument
161 return f"[type:{self.type} len:{self._len}] {self.raw}"
165 def __init__(self, msg): argument
166 self.attrs = []
172 self.attrs.append(attr)
174 def __iter__(self): argument
175 yield from self.attrs
177 def __repr__(self): argument
179 for a in self.attrs:
187 def __init__(self, msg, offset, attr_space=None): argument
188 self.hdr = msg[offset:offset + 16]
190 self.nl_len, self.nl_type, self.nl_flags, self.nl_seq, self.nl_portid = \
191 struct.unpack("IHHII", self.hdr)
193 self.raw = msg[offset + 16:offset + self.nl_len]
195 self.error = 0
196 self.done = 0
199 if self.nl_type == Netlink.NLMSG_ERROR:
200 self.error = struct.unpack("i", self.raw[0:4])[0]
201 self.done = 1
203 elif self.nl_type == Netlink.NLMSG_DONE:
204 self.done = 1
207 self.extack = None
208 if self.nl_flags & Netlink.NLM_F_ACK_TLVS and extack_off:
209 self.extack = dict()
210 extack_attrs = NlAttrs(self.raw[extack_off:])
213 self.extack['msg'] = extack.as_strz()
215 self.extack['miss-type'] = extack.as_scalar('u32')
217 self.extack['miss-nest'] = extack.as_scalar('u32')
219 self.extack['bad-attr-offs'] = extack.as_scalar('u32')
221 if 'unknown' not in self.extack:
222 self.extack['unknown'] = []
223 self.extack['unknown'].append(extack)
227 if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
228 miss_type = self.extack['miss-type']
234 self.extack['miss-type'] = desc
236 def cmd(self): argument
237 return self.nl_type
239 def __repr__(self): argument
240 …msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl…
241 if self.error:
242 msg += '\terror: ' + str(self.error)
243 if self.extack:
244 msg += '\textack: ' + repr(self.extack)
249 def __init__(self, data, attr_space=None): argument
250 self.msgs = []
256 self.msgs.append(msg)
258 def __iter__(self): argument
259 yield from self.msgs
328 def __init__(self, nl_msg): argument
329 self.nl = nl_msg
330 self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
331 self.raw = nl_msg.raw[4:]
333 def cmd(self): argument
334 return self.genl_cmd
336 def __repr__(self): argument
337 msg = repr(self.nl)
338 msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
339 for a in self.raw_attrs:
345 def __init__(self, family_name, proto_num): argument
346 self.family_name = family_name
347 self.proto_num = proto_num
349 def _message(self, nl_type, nl_flags, seq=None): argument
355 def message(self, flags, command, version, seq=None): argument
356 return self._message(command, flags, seq)
358 def _decode(self, nl_msg): argument
361 def decode(self, ynl, nl_msg): argument
362 msg = self._decode(nl_msg)
370 def get_mcast_id(self, mcast_name, mcast_groups): argument
377 def __init__(self, family_name): argument
384 self.genl_family = genl_family_name_to_id[family_name]
385 self.family_id = genl_family_name_to_id[family_name]['id']
387 def message(self, flags, command, version, seq=None): argument
388 nlmsg = self._message(self.family_id, flags, seq)
392 def _decode(self, nl_msg): argument
395 def get_mcast_id(self, mcast_name, mcast_groups): argument
396 if mcast_name not in self.genl_family['mcast']:
398 return self.genl_family['mcast'][mcast_name]
407 def __init__(self, def_path, schema=None): argument
410 self.include_raw = False
413 if self.proto == "netlink-raw":
414 self.nlproto = NetlinkProtocol(self.yaml['name'],
415 self.yaml['protonum'])
417 self.nlproto = GenlProtocol(self.yaml['name'])
419 raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
421 self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
422 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
423 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
424 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
426 self.async_msg_ids = set()
427 self.async_msg_queue = []
429 for msg in self.msgs.values():
431 self.async_msg_ids.add(msg.rsp_value)
433 for op_name, op in self.ops.items():
434 bound_f = functools.partial(self._op, op_name)
435 setattr(self, op.ident_name, bound_f)
438 def ntf_subscribe(self, mcast_name): argument
439 mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
440 self.sock.bind((0, 0))
441 self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
444 def _add_attr(self, space, name, value): argument
446 attr = self.attr_sets[space][name]
454 attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue)
472 pad = b'\x00' * ((4 - len(attr_payload) % 4) % 4)
475 def _decode_enum(self, raw, attr_spec): argument
476 enum = self.consts[attr_spec['enum']]
477 if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
489 def _decode_binary(self, attr, attr_spec): argument
491 members = self.consts[attr_spec.struct_name]
495 decoded[m.name] = self._decode_enum(decoded[m.name], m)
504 def _decode_array_nest(self, attr, attr_spec): argument
511 subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
515 def _decode(self, attrs, space): argument
516 attr_space = self.attr_sets[space]
524 subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'])
529 decoded = self._decode_binary(attr, attr_spec)
534 elif attr_spec["type"] == 'array-nest':
535 decoded = self._decode_array_nest(attr, attr_spec)
540 decoded = self._decode_enum(decoded, attr_spec)
551 def _decode_extack_path(self, attrs, attr_set, offset, target): argument
568 subpath = self._decode_extack_path(NlAttrs(attr.raw),
569 self.attr_sets[attr_spec['nested-attributes']],
577 def _decode_extack(self, request, op, extack): argument
578 if 'bad-attr-offs' not in extack:
581 msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set))
582 offset = 20 + self._fixed_header_size(op)
583 path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
584 extack['bad-attr-offs'])
586 del extack['bad-attr-offs']
587 extack['bad-attr'] = path
589 def _fixed_header_size(self, op): argument
591 fixed_header_members = self.consts[op.fixed_header].members
600 def _decode_fixed_header(self, msg, name): argument
601 fixed_header_members = self.consts[name].members
609 value = self._decode_enum(value, m)
613 def handle_ntf(self, decoded): argument
615 if self.include_raw:
617 op = self.rsp_by_value[decoded.cmd()]
618 attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
620 attrs.update(self._decode_fixed_header(decoded, op.fixed_header))
624 self.async_msg_queue.append(msg)
626 def check_ntf(self): argument
629 reply = self.sock.recv(128 * 1024, socket.MSG_DONTWAIT)
636 print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
643 decoded = self.nlproto.decode(self, nl_msg)
644 if decoded.cmd() not in self.async_msg_ids:
648 self.handle_ntf(decoded)
650 def operation_do_attributes(self, name): argument
655 op = self.find_operation(name)
661 def _op(self, method, vals, flags, dump=False): argument
662 op = self.ops[method]
671 msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
674 fixed_header_members = self.consts[op.fixed_header].members
680 msg += self._add_attr(op.attr_set.name, name, value)
683 self.sock.send(msg, 0)
688 reply = self.sock.recv(128 * 1024)
692 self._decode_extack(msg, op, nl_msg.extack)
703 decoded = self.nlproto.decode(self, nl_msg)
707 if decoded.cmd() in self.async_msg_ids:
708 self.handle_ntf(decoded)
714 rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
716 rsp_msg.update(self._decode_fixed_header(decoded, op.fixed_header))
725 def do(self, method, vals, flags): argument
726 return self._op(method, vals, flags)
728 def dump(self, method, vals): argument
729 return self._op(method, vals, [], dump=True)