# Packet library API Reference

## Packet class

class os_ken.lib.packet.packet.Packet(data=None, protocols=None, parse_cls=<class 'os_ken.lib.packet.ethernet.ethernet'>)

  A packet decoder/encoder class.

  An instance is used to either decode or encode a single packet.

  data is a bytearray to describe a raw datagram to decode. When decoding, a Packet object is iteratable. Iterated values are protocol (ethernet, ipv4, ...) headers and the payload. Protocol headers are instances of subclass of packet_base.PacketBase. The payload is a bytearray. They are iterated in on-wire order.

  data should be omitted when encoding a packet.

  add_protocol(proto)

    Register a protocol proto for this packet.

    This method is legal only when encoding a packet.

    When encoding a packet, register a protocol (ethernet, ipv4, ...) header to add to this packet. Protocol headers should be registered in on-wire order before calling self.serialize.

  classmethod from_jsondict(dict_, decode_string=<function b64decode>, **additional_args)

    Create an instance from a JSON style dict.

    Instantiate this class with parameters specified by the dict.

    This method takes the following arguments.

    Argument | Descrpition
    --- | ---
    dict_ | A dictionary which describes the parameters. For example, {"Param1": 100, "Param2": 200}
    decode_string | (Optional) specify how to decode strings. The default is base64. This argument is used only for attributes which don't have explicit type annotations in _TYPE class attribute.
    additional_args | (Optional) Additional kwargs for constructor.

  get_protocol(protocol)

    Returns the firstly found protocol that matches to the specified protocol.

  get_protocols(protocol)

    Returns a list of protocols that matches to the specified protocol.

  serialize()

    Encode a packet and store the resulted bytearray in self.data.

    This method is legal only when encoding a packet.

## Stream Parser class

class os_ken.lib.packet.stream_parser.StreamParser

  Streaming parser base class.

  An instance of a subclass of this class is used to extract messages from a raw byte stream.

  It's designed to be used for data read from a transport which doesn't preserve message boundaries. A typical example of such a transport is TCP.

  exception TooSmallException
  parse(data)

    Tries to extract messages from a raw byte stream.

    The data argument would be python bytes newly read from the input stream.

    Returns an ordered list of extracted messages. It can be an empty list.

    The rest of data which doesn't produce a complete message is kept internally and will be used when more data is come. I.e. next time this method is called again.

  abstractmethod try_parse(q)

    Try to extract a message from the given bytes.

    This is an override point for subclasses.

    This method tries to extract a message from bytes given by the argument.

    Raises TooSmallException if the given data is not enough to extract a complete message but there's still a chance to extract a message if more data is come later.

List of the sub-classes:

- `os_ken.lib.packet.bgp.StreamParser`

## Protocol Header classes

- Packet Base Class
  - `PacketBase`
    - `PacketBase.get_packet_type()`
    - `PacketBase.parser()`
    - `PacketBase.register_packet_type()`
    - `PacketBase.serialize()`
- ARP
  - `arp`
    - `arp.parser()`
    - `arp.serialize()`
  - `arp_ip()`
- BFD
  - `BFDAuth`
    - `BFDAuth.parser_hdr()`
    - `BFDAuth.serialize_hdr()`
  - `KeyedMD5`
    - `KeyedMD5.authenticate()`
    - `KeyedMD5.serialize()`
  - `KeyedSHA1`
    - `KeyedSHA1.authenticate()`
    - `KeyedSHA1.serialize()`
  - `MeticulousKeyedMD5`
  - `MeticulousKeyedSHA1`
  - `SimplePassword`
    - `SimplePassword.authenticate()`
    - `SimplePassword.serialize()`
  - `bfd`
    - `bfd.authenticate()`
    - `bfd.pack()`
    - `bfd.parser()`
    - `bfd.serialize()`
- BGP
  - `AdminReset`
    - `AdminReset.CODE`
    - `AdminReset.SUB_CODE`
  - `AdminShutdown`
    - `AdminShutdown.CODE`
    - `AdminShutdown.SUB_CODE`
  - `AttrFlagError`
    - `AttrFlagError.CODE`
    - `AttrFlagError.SUB_CODE`
  - `AttrLenError`
    - `AttrLenError.CODE`
    - `AttrLenError.SUB_CODE`
  - `AuthFailure`
    - `AuthFailure.CODE`
    - `AuthFailure.SUB_CODE`
  - `BGPEvpnEsImportRTExtendedCommunity`
  - `BGPEvpnEsiLabelExtendedCommunity`
  - `BGPEvpnMacMobilityExtendedCommunity`
  - `BGPFlowSpecRedirectCommunity`
  - `BGPFlowSpecTPIDActionCommunity`
  - `BGPFlowSpecTrafficActionCommunity`
  - `BGPFlowSpecTrafficMarkingCommunity`
  - `BGPFlowSpecTrafficRateCommunity`
  - `BGPFlowSpecVlanActionCommunity`
  - `BGPKeepAlive`
    - `BGPKeepAlive.parser()`
  - `BGPMessage`
    - `BGPMessage.parser()`
    - `BGPMessage.serialize()`
  - `BGPNotification`
    - `BGPNotification.parser()`
  - `BGPOpen`
    - `BGPOpen.parser()`
  - `BGPPathAttributePmsiTunnel`
    - `BGPPathAttributePmsiTunnel.from_jsondict()`
  - `BGPRouteRefresh`
    - `BGPRouteRefresh.parser()`
  - `BGPUpdate`
    - `BGPUpdate.parser()`
  - `BadBgpId`
    - `BadBgpId.CODE`
    - `BadBgpId.SUB_CODE`
  - `BadLen`
    - `BadLen.CODE`
    - `BadLen.SUB_CODE`
  - `BadMsg`
    - `BadMsg.CODE`
    - `BadMsg.SUB_CODE`
  - `BadNotification`
    - `BadNotification.SEND_ERROR`
  - `BadPeerAs`
    - `BadPeerAs.CODE`
    - `BadPeerAs.SUB_CODE`
  - `BgpExc`
    - `BgpExc.CODE`
    - `BgpExc.SEND_ERROR`
    - `BgpExc.SUB_CODE`
  - `CollisionResolution`
    - `CollisionResolution.CODE`
    - `CollisionResolution.SUB_CODE`
  - `ConnRejected`
    - `ConnRejected.CODE`
    - `ConnRejected.SUB_CODE`
  - `EvpnASBasedEsi`
  - `EvpnArbitraryEsi`
  - `EvpnEsi`
  - `EvpnEthernetAutoDiscoveryNLRI`
  - `EvpnEthernetSegmentNLRI`
  - `EvpnInclusiveMulticastEthernetTagNLRI`
  - `EvpnIpPrefixNLRI`
  - `EvpnL2BridgeEsi`
  - `EvpnLACPEsi`
  - `EvpnMacBasedEsi`
  - `EvpnMacIPAdvertisementNLRI`
  - `EvpnNLRI`
  - `EvpnRouterIDEsi`
  - `EvpnUnknownEsi`
  - `EvpnUnknownNLRI`
  - `FiniteStateMachineError`
    - `FiniteStateMachineError.CODE`
    - `FiniteStateMachineError.SUB_CODE`
  - `FlowSpecComponentUnknown`
  - `FlowSpecDSCP`
  - `FlowSpecDestPort`
  - `FlowSpecDestPrefix`
  - `FlowSpecDestinationMac`
  - `FlowSpecEtherType`
  - `FlowSpecFragment`
  - `FlowSpecIPProtocol`
  - `FlowSpecIPv4NLRI`
    - `FlowSpecIPv4NLRI.from_user()`
  - `FlowSpecIPv6DestPrefix`
  - `FlowSpecIPv6FlowLabel`
  - `FlowSpecIPv6Fragment`
  - `FlowSpecIPv6NLRI`
    - `FlowSpecIPv6NLRI.from_user()`
  - `FlowSpecIPv6SrcPrefix`
  - `FlowSpecIcmpCode`
  - `FlowSpecIcmpType`
  - `FlowSpecInnerVLANCoS`
  - `FlowSpecInnerVLANID`
  - `FlowSpecL2VPNNLRI`
    - `FlowSpecL2VPNNLRI.from_user()`
  - `FlowSpecLLCControl`
  - `FlowSpecLLCDSAP`
  - `FlowSpecLLCSSAP`
  - `FlowSpecNextHeader`
  - `FlowSpecPacketLen`
  - `FlowSpecPort`
  - `FlowSpecSNAP`
  - `FlowSpecSourceMac`
  - `FlowSpecSrcPort`
  - `FlowSpecSrcPrefix`
  - `FlowSpecTCPFlags`
  - `FlowSpecVLANCoS`
  - `FlowSpecVLANID`
  - `FlowSpecVPNv4NLRI`
    - `FlowSpecVPNv4NLRI.from_user()`
  - `FlowSpecVPNv6NLRI`
    - `FlowSpecVPNv6NLRI.from_user()`
  - `HoldTimerExpired`
    - `HoldTimerExpired.CODE`
    - `HoldTimerExpired.SUB_CODE`
  - `InvalidNetworkField`
    - `InvalidNetworkField.CODE`
    - `InvalidNetworkField.SUB_CODE`
  - `InvalidNextHop`
    - `InvalidNextHop.CODE`
    - `InvalidNextHop.SUB_CODE`
  - `InvalidOriginError`
    - `InvalidOriginError.CODE`
    - `InvalidOriginError.SUB_CODE`
  - `MalformedAsPath`
    - `MalformedAsPath.CODE`
    - `MalformedAsPath.SUB_CODE`
  - `MalformedAttrList`
    - `MalformedAttrList.CODE`
    - `MalformedAttrList.SUB_CODE`
  - `MalformedOptionalParam`
    - `MalformedOptionalParam.CODE`
    - `MalformedOptionalParam.SUB_CODE`
  - `MaxPrefixReached`
    - `MaxPrefixReached.CODE`
    - `MaxPrefixReached.SUB_CODE`
  - `MissingWellKnown`
    - `MissingWellKnown.CODE`
    - `MissingWellKnown.SUB_CODE`
  - `NotSync`
    - `NotSync.CODE`
    - `NotSync.SUB_CODE`
  - `OptAttrError`
    - `OptAttrError.CODE`
    - `OptAttrError.SUB_CODE`
  - `OtherConfChange`
    - `OtherConfChange.CODE`
    - `OtherConfChange.SUB_CODE`
  - `OutOfResource`
    - `OutOfResource.CODE`
    - `OutOfResource.SUB_CODE`
  - `PeerDeConfig`
    - `PeerDeConfig.CODE`
    - `PeerDeConfig.SUB_CODE`
  - `PmsiTunnelIdUnknown`
  - `RouteTargetMembershipNLRI`
  - `RoutingLoop`
    - `RoutingLoop.CODE`
    - `RoutingLoop.SUB_CODE`
  - `StreamParser`
    - `StreamParser.try_parse()`
  - `UnRegWellKnowAttr`
    - `UnRegWellKnowAttr.CODE`
    - `UnRegWellKnowAttr.SUB_CODE`
  - `UnacceptableHoldTime`
    - `UnacceptableHoldTime.CODE`
    - `UnacceptableHoldTime.SUB_CODE`
  - `UnsupportedOptParam`
    - `UnsupportedOptParam.CODE`
    - `UnsupportedOptParam.SUB_CODE`
  - `UnsupportedVersion`
    - `UnsupportedVersion.CODE`
    - `UnsupportedVersion.SUB_CODE`
- BMP
  - `BMPInitiation`
    - `BMPInitiation.parser()`
  - `BMPMessage`
    - `BMPMessage.parser()`
    - `BMPMessage.serialize()`
  - `BMPPeerDownNotification`
    - `BMPPeerDownNotification.parser()`
  - `BMPPeerMessage`
    - `BMPPeerMessage.parser()`
  - `BMPPeerUpNotification`
    - `BMPPeerUpNotification.parser()`
  - `BMPRouteMonitoring`
    - `BMPRouteMonitoring.parser()`
  - `BMPStatisticsReport`
    - `BMPStatisticsReport.parser()`
  - `BMPTermination`
    - `BMPTermination.parser()`
- BPDU
  - `ConfigurationBPDUs`
    - `ConfigurationBPDUs.parser()`
    - `ConfigurationBPDUs.serialize()`
  - `RstBPDUs`
    - `RstBPDUs.parser()`
    - `RstBPDUs.serialize()`
  - `TopologyChangeNotificationBPDUs`
    - `TopologyChangeNotificationBPDUs.parser()`
  - `bpdu`
    - `bpdu.parser()`
    - `bpdu.serialize()`
- CFM
  - `cc_message`
  - `cfm`
    - `cfm.parser()`
    - `cfm.serialize()`
  - `data_tlv`
  - `interface_status_tlv`
  - `link_trace_message`
  - `link_trace_reply`
  - `loopback_message`
  - `loopback_reply`
  - `ltm_egress_identifier_tlv`
  - `ltr_egress_identifier_tlv`
  - `organization_specific_tlv`
  - `port_status_tlv`
  - `reply_egress_tlv`
  - `reply_ingress_tlv`
  - `sender_id_tlv`
- DHCP
  - `dhcp`
    - `dhcp.parser()`
    - `dhcp.serialize()`
  - `option`
  - `options`
- DHCP6
  - `dhcp6`
    - `dhcp6.parser()`
    - `dhcp6.serialize()`
  - `option`
  - `options`
- Ethernet
  - `ethernet`
    - `ethernet.get_packet_type()`
    - `ethernet.parser()`
    - `ethernet.serialize()`
- Geneve
  - `Option`
  - `OptionDataUnknown`
  - `geneve`
    - `geneve.parser()`
    - `geneve.serialize()`
- GRE
  - `gre`
    - `gre.parser()`
    - `gre.serialize()`
  - `nvgre()`
- ICMP
  - `TimeExceeded`
  - `dest_unreach`
  - `echo`
  - `icmp`
    - `icmp.parser()`
    - `icmp.serialize()`
- ICMPv6
  - `echo`
  - `icmpv6`
    - `icmpv6.parser()`
    - `icmpv6.serialize()`
  - `mld`
  - `mldv2_query`
  - `mldv2_report`
  - `mldv2_report_group`
  - `nd_neighbor`
  - `nd_option_mtu`
  - `nd_option_pi`
  - `nd_option_sla`
  - `nd_option_tla`
  - `nd_router_advert`
  - `nd_router_solicit`
- IGMP
  - `igmp`
    - `igmp.parser()`
    - `igmp.serialize()`
  - `igmpv3_query`
    - `igmpv3_query.parser()`
    - `igmpv3_query.serialize()`
  - `igmpv3_report`
    - `igmpv3_report.parser()`
    - `igmpv3_report.serialize()`
  - `igmpv3_report_group`
- IPv4
  - `ipv4`
    - `ipv4.parser()`
    - `ipv4.serialize()`
- IPv6
  - `auth`
  - `dst_opts`
  - `fragment`
  - `header`
  - `hop_opts`
  - `ipv6`
    - `ipv6.parser()`
    - `ipv6.serialize()`
  - `opt_header`
  - `option`
  - `routing`
  - `routing_type3`
- LLC
  - `ControlFormatI`
  - `ControlFormatS`
  - `ControlFormatU`
  - `llc`
    - `llc.parser()`
    - `llc.serialize()`
- LLDP
  - `ChassisID`
  - `End`
  - `ManagementAddress`
  - `OrganizationallySpecific`
  - `PortDescription`
  - `PortID`
  - `SystemCapabilities`
  - `SystemDescription`
  - `SystemName`
  - `TTL`
  - `lldp`
    - `lldp.parser()`
    - `lldp.serialize()`
- MPLS
  - `label_from_bin()`
  - `label_to_bin()`
  - `mpls`
    - `mpls.parser()`
    - `mpls.serialize()`
- OpenFlow
  - `OFPUnparseableMsg`
  - `openflow`
    - `openflow.parser()`
    - `openflow.serialize()`
- OSPF
  - `InvalidChecksum`
  - `OSPFDBDesc`
    - `OSPFDBDesc.parser()`
  - `OSPFHello`
    - `OSPFHello.parser()`
  - `OSPFLSAck`
    - `OSPFLSAck.parser()`
  - `OSPFLSReq`
    - `OSPFLSReq.parser()`
  - `OSPFLSUpd`
    - `OSPFLSUpd.parser()`
  - `OSPFMessage`
    - `OSPFMessage.parser()`
    - `OSPFMessage.serialize()`
  - `ospf`
- PBB
  - `itag`
    - `itag.parser()`
    - `itag.serialize()`
- SCTP
  - `cause_cookie_while_shutdown`
  - `cause_invalid_param`
  - `cause_invalid_stream_id`
  - `cause_missing_param`
  - `cause_no_userdata`
  - `cause_out_of_resource`
  - `cause_protocol_violation`
  - `cause_restart_with_new_addr`
  - `cause_stale_cookie`
  - `cause_unrecognized_chunk`
  - `cause_unrecognized_param`
  - `cause_unresolvable_addr`
  - `cause_user_initiated_abort`
  - `chunk_abort`
  - `chunk_cookie_ack`
  - `chunk_cookie_echo`
  - `chunk_cwr`
  - `chunk_data`
  - `chunk_ecn_echo`
  - `chunk_error`
  - `chunk_heartbeat`
  - `chunk_heartbeat_ack`
  - `chunk_init`
  - `chunk_init_ack`
  - `chunk_sack`
  - `chunk_shutdown`
  - `chunk_shutdown_ack`
  - `chunk_shutdown_complete`
  - `param_cookie_preserve`
  - `param_ecn`
  - `param_heartbeat`
  - `param_host_addr`
  - `param_ipv4`
  - `param_ipv6`
  - `param_state_cookie`
  - `param_supported_addr`
  - `param_unrecognized_param`
  - `sctp`
    - `sctp.parser()`
    - `sctp.serialize()`
- Slow
  - `lacp`
    - `lacp.parser()`
    - `lacp.serialize()`
  - `slow`
    - `slow.parser()`
- TCP
  - `tcp`
    - `tcp.has_flags()`
    - `tcp.parser()`
    - `tcp.serialize()`
- UDP
  - `udp`
    - `udp.get_packet_type()`
    - `udp.parser()`
    - `udp.serialize()`
- VLAN
  - `svlan`
    - `svlan.get_packet_type()`
  - `vlan`
    - `vlan.get_packet_type()`
- VRRP
  - `vrrp`
    - `vrrp.create_packet()`
    - `vrrp.parser()`
    - `vrrp.serialize()`
  - `vrrpv2`
    - `vrrpv2.create()`
    - `vrrpv2.parser()`
  - `vrrpv3`
    - `vrrpv3.create()`
    - `vrrpv3.parser()`
- VXLAN
  - `vni_from_bin()`
  - `vni_to_bin()`
  - `vxlan`
    - `vxlan.parser()`
    - `vxlan.serialize()`
- Zebra
  - `InterfaceLinkParams`
  - `NextHopBlackhole`
  - `NextHopIFIndex`
  - `NextHopIFName`
  - `NextHopIPv4`
  - `NextHopIPv4IFIndex`
  - `NextHopIPv4IFName`
  - `NextHopIPv6`
  - `NextHopIPv6IFIndex`
  - `NextHopIPv6IFName`
  - `RegisteredNexthop`
  - `ZebraBfdClientRegister`
  - `ZebraBfdDestinationDeregister`
  - `ZebraBfdDestinationRegister`
  - `ZebraBfdDestinationReply`
  - `ZebraBfdDestinationUpdate`
  - `ZebraHello`
  - `ZebraIPv4ImportLookup`
  - `ZebraIPv4NexthopAdd`
  - `ZebraIPv4NexthopDelete`
  - `ZebraIPv4NexthopLookup`
  - `ZebraIPv4NexthopLookupMRib`
  - `ZebraIPv4RouteAdd`
  - `ZebraIPv4RouteDelete`
  - `ZebraIPv4RouteIPv6NexthopAdd`
  - `ZebraIPv6ImportLookup`
  - `ZebraIPv6NexthopAdd`
  - `ZebraIPv6NexthopDelete`
  - `ZebraIPv6NexthopLookup`
  - `ZebraIPv6RouteAdd`
  - `ZebraIPv6RouteDelete`
  - `ZebraImportCheckUpdate`
  - `ZebraImportRouteRegister`
  - `ZebraImportRouteUnregister`
  - `ZebraInterfaceAdd`
  - `ZebraInterfaceAddressAdd`
  - `ZebraInterfaceAddressDelete`
  - `ZebraInterfaceBfdDestinationUpdate`
  - `ZebraInterfaceDelete`
  - `ZebraInterfaceDisableRadv`
  - `ZebraInterfaceDown`
  - `ZebraInterfaceEnableRadv`
  - `ZebraInterfaceLinkParams`
  - `ZebraInterfaceNbrAddressAdd`
  - `ZebraInterfaceNbrAddressDelete`
  - `ZebraInterfaceUp`
  - `ZebraInterfaceVrfUpdate`
  - `ZebraMessage`
    - `ZebraMessage.parser()`
    - `ZebraMessage.serialize()`
  - `ZebraMplsLabelsAdd`
  - `ZebraMplsLabelsDelete`
  - `ZebraNexthopRegister`
  - `ZebraNexthopUnregister`
  - `ZebraNexthopUpdate`
  - `ZebraRedistributeAdd`
  - `ZebraRedistributeDefaultAdd`
  - `ZebraRedistributeDefaultDelete`
  - `ZebraRedistributeDelete`
  - `ZebraRedistributeIPv4Add`
  - `ZebraRedistributeIPv4Delete`
  - `ZebraRedistributeIPv6Add`
  - `ZebraRedistributeIPv6Delete`
  - `ZebraRouterIDAdd`
  - `ZebraRouterIDDelete`
  - `ZebraRouterIDUpdate`
  - `ZebraUnknownMessage`
  - `ZebraVrfAdd`
  - `ZebraVrfDelete`
  - `ZebraVrfUnregister`
  - `zebra`

this page last updated: 2026-04-10 03:11:34

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.

found an error? report a bug