code: def bfd_packet(src_mac, dst_mac, src_ip, dst_ip, ipv4_id,
                     src_port, dst_port,
                     diag=0, state=0, flags=0, detect_mult=0,
                     my_discr=0, your_discr=0, desired_min_tx_interval=0,
                     required_min_rx_interval=0,
                     required_min_echo_rx_interval=0,
                     auth_cls=None):
          
          # Generate ethernet header first.
          pkt = packet.Packet()
          eth_pkt = ethernet.ethernet(dst_mac, src_mac, ETH_TYPE_IP)
          pkt.add_protocol(eth_pkt)
  
          # IPv4 encapsulation
          # set ToS to 192 (Network control/CS6)
          # set TTL to 255 (RFC5881 Section 5.)
          ipv4_pkt = ipv4.ipv4(proto=inet.IPPROTO_UDP, src=src_ip, dst=dst_ip,
                               tos=192, identification=ipv4_id, ttl=255)
          pkt.add_protocol(ipv4_pkt)
  
          # UDP encapsulation
          udp_pkt = udp.udp(src_port=src_port, dst_port=dst_port)
          pkt.add_protocol(udp_pkt)
  
          # BFD payload
          bfd_pkt = bfd.bfd(
              ver=1, diag=diag, state=state, flags=flags,
              detect_mult=detect_mult,
              my_discr=my_discr, your_discr=your_discr,
              desired_min_tx_interval=desired_min_tx_interval,
              required_min_rx_interval=required_min_rx_interval,
              required_min_echo_rx_interval=required_min_echo_rx_interval,
              auth_cls=auth_cls)
          pkt.add_protocol(bfd_pkt)
  
          pkt.serialize()
          return pkt.data
docs: Generate BFD packet with Ethernet/IPv4/UDP encapsulated.