github.com/contiv/libOpenflow@v0.0.0-20210609050114-d967b14cc688/protocol/lldp.go (about)

     1  package protocol
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  )
     7  
     8  type LLDP struct {
     9  	Chassis ChassisTLV
    10  	Port    PortTLV
    11  	TTL     TTLTLV
    12  }
    13  
    14  func (d *LLDP) Len() (n uint16) {
    15  	return 15
    16  }
    17  
    18  func (d *LLDP) Read(b []byte) (n int, err error) {
    19  	m, o, p := 0, 0, 0
    20  	if m, err = d.Chassis.Read(b); m == 0 {
    21  		return
    22  	}
    23  	n += m
    24  	if o, err = d.Port.Read(b); o == 0 {
    25  		return
    26  	}
    27  	n += o
    28  	if p, err = d.Chassis.Read(b); p == 0 {
    29  		return
    30  	}
    31  	n += p
    32  	return
    33  }
    34  
    35  func (d *LLDP) Write(b []byte) (n int, err error) {
    36  	m, o, p := 0, 0, 0
    37  	if m, err = d.Chassis.Write(b); m == 0 {
    38  		return
    39  	}
    40  	n += m
    41  	if o, err = d.Port.Write(b[n:]); o == 0 {
    42  		return
    43  	}
    44  	n += o
    45  	if p, err = d.Chassis.Write(b[n:]); p == 0 {
    46  		return
    47  	}
    48  	n += p
    49  	return
    50  }
    51  
    52  // Chassis ID subtypes
    53  const (
    54  	_ = iota
    55  	CH_CHASSIS_COMPONENT
    56  	CH_IFACE_ALIAS
    57  	CH_PORT_COMPONENT
    58  	CH_MAC_ADDR
    59  	CH_NET_ADDR
    60  	CH_IFACE_NAME
    61  	CH_LOCAL_ASSGN
    62  )
    63  
    64  type ChassisTLV struct {
    65  	Type    uint8
    66  	Length  uint16
    67  	Subtype uint8
    68  	Data    []uint8
    69  }
    70  
    71  func (t *ChassisTLV) Read(b []byte) (n int, err error) {
    72  	buf := new(bytes.Buffer)
    73  	var tni uint16 = 0
    74  	typeAndLen := (tni | uint16(t.Type)<<9) + (tni | uint16(t.Length))
    75  	binary.Write(buf, binary.BigEndian, typeAndLen)
    76  	binary.Write(buf, binary.BigEndian, t.Subtype)
    77  	binary.Write(buf, binary.BigEndian, t.Data)
    78  	n, err = buf.Read(b)
    79  	return
    80  }
    81  
    82  func (t *ChassisTLV) Write(b []byte) (n int, err error) {
    83  	buf := bytes.NewBuffer(b)
    84  	var typeAndLen uint16 = 0
    85  	if err = binary.Read(buf, binary.BigEndian, &typeAndLen); err != nil {
    86  		return
    87  	}
    88  	n += 2
    89  	t.Type = uint8(typeAndLen >> 9)
    90  	t.Length = uint16(uint16(0x01ff) & typeAndLen)
    91  	if err = binary.Read(buf, binary.BigEndian, &t.Subtype); err != nil {
    92  		return
    93  	}
    94  	n += 1
    95  	t.Data = make([]uint8, t.Length)
    96  	if err = binary.Read(buf, binary.BigEndian, &t.Data); err != nil {
    97  		return
    98  	}
    99  	n += int(t.Length)
   100  	return
   101  }
   102  
   103  // Port ID subtypes
   104  const (
   105  	_ = iota
   106  	PT_IFACE_ALIAS
   107  	PT_PORT_COMPONENT
   108  	PT_MAC_ADDR
   109  	PT_NET_ADDR
   110  	PT_IFACE_NAME
   111  	PT_CIRCUIT_ID
   112  	PT_LOCAL_ASSGN
   113  )
   114  
   115  type PortTLV struct {
   116  	Type    uint8  //7bits
   117  	Length  uint16 //9bits
   118  	Subtype uint8
   119  	Data    []uint8
   120  }
   121  
   122  func (t *PortTLV) Read(b []byte) (n int, err error) {
   123  	buf := new(bytes.Buffer)
   124  	var tni uint16 = 0
   125  	typeAndLen := (tni | uint16(t.Type)<<9) + (tni | uint16(t.Length))
   126  	binary.Write(buf, binary.BigEndian, typeAndLen)
   127  	binary.Write(buf, binary.BigEndian, t.Subtype)
   128  	binary.Write(buf, binary.BigEndian, t.Data)
   129  	n, err = buf.Read(b)
   130  	return
   131  }
   132  
   133  func (t *PortTLV) Write(b []byte) (n int, err error) {
   134  	buf := bytes.NewBuffer(b)
   135  	var typeAndLen uint16 = 0
   136  	if err = binary.Read(buf, binary.BigEndian, &typeAndLen); err != nil {
   137  		return
   138  	}
   139  	n += 2
   140  	t.Type = uint8(typeAndLen >> 9)
   141  	t.Length = uint16(uint16(0x01ff) & typeAndLen)
   142  	if err = binary.Read(buf, binary.BigEndian, &t.Subtype); err != nil {
   143  		return
   144  	}
   145  	n += 1
   146  	t.Data = make([]uint8, t.Length)
   147  	if err = binary.Read(buf, binary.BigEndian, &t.Data); err != nil {
   148  		return
   149  	}
   150  	n += int(t.Length)
   151  	return
   152  }
   153  
   154  type TTLTLV struct {
   155  	Type    uint8  //7 bits
   156  	Length  uint16 //9 bits
   157  	Seconds uint16
   158  }
   159  
   160  func (t *TTLTLV) Read(b []byte) (n int, err error) {
   161  	buf := new(bytes.Buffer)
   162  	var tni uint16 = 0
   163  	typeAndLen := (tni | uint16(t.Type)<<9) + (tni | uint16(t.Length))
   164  	binary.Write(buf, binary.BigEndian, typeAndLen)
   165  	binary.Write(buf, binary.BigEndian, t.Seconds)
   166  	n, err = buf.Read(b)
   167  	return
   168  }
   169  
   170  func (t *TTLTLV) Write(b []byte) (n int, err error) {
   171  	buf := bytes.NewBuffer(b)
   172  	var typeAndLen uint16 = 0
   173  	if err = binary.Read(buf, binary.BigEndian, &typeAndLen); err != nil {
   174  		return
   175  	}
   176  	n += 2
   177  	t.Type = uint8(typeAndLen >> 9)
   178  	t.Length = uint16(uint16(0x01ff) & typeAndLen)
   179  	if err = binary.Read(buf, binary.BigEndian, &t.Seconds); err != nil {
   180  		return
   181  	}
   182  	n += 2
   183  	return
   184  }