Skip to content

Commit 28cb5f1

Browse files
committed
some cleanup and doc updates
1 parent 94e3508 commit 28cb5f1

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed

afconn_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func getVLANsFromAncDataAFPkt(existList []uint16, auxdata []interface{}) []uint1
8383
return r
8484
}
8585

86+
// NewRawSocketRelayPcap creates a new RawSocketRelay instance using AF_PACKET socket , bound to the interface ifname,
87+
// optionally with RelayOption functions.
88+
// This function will put the interface in promisc mode, which means it requires root privilage.
8689
func NewRawSocketRelay(parentctx context.Context, ifname string, options ...RelayOption) (*RawSocketRelay, error) {
8790
//NOTE:interface must be put in promisc mode, otherwise only pkt with real mac will be received
8891
err := SetPromisc(ifname)
@@ -102,7 +105,7 @@ func NewRawSocketRelay(parentctx context.Context, ifname string, options ...Rela
102105
if err != nil {
103106
return nil, fmt.Errorf("failed to create rawsocketrelay,%w", err)
104107
}
105-
return NewRawSocketRelayWithRelayConn(parentctx, ifname, conn, options...)
108+
return newRawSocketRelayWithRelayConn(parentctx, ifname, conn, options...)
106109

107110
}
108111

etherconn_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func getRawRelay(ctx context.Context, relayType etherconn.RelayType, ifname stri
5151

5252
switch relayType {
5353
case etherconn.RelayTypePCAP:
54-
return etherconn.NewRawSocketRelayPcap(context.Background(), ifname, mods...)
54+
return etherconn.NewRawSocketRelayPcap(ctx, ifname, mods...)
5555
case etherconn.RelayTypeAFP:
56-
return etherconn.NewRawSocketRelay(context.Background(), ifname, mods...)
56+
return etherconn.NewRawSocketRelay(ctx, ifname, mods...)
5757

5858
}
5959
return nil, fmt.Errorf("%v is not a supported raw relay type", relayType)

nd/nd.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ func (v6nd *IPv6ND) sendNA(reqsrc, target net.IP, targetmac, dstmac net.Hardware
143143
}
144144
respicmp6Layer.SetNetworkLayerForChecksum(iplayer)
145145
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
146-
gopacket.SerializeLayers(buf, opts,
146+
err = gopacket.SerializeLayers(buf, opts,
147147
iplayer,
148148
respicmp6Layer,
149149
resp)
150-
150+
if err != nil {
151+
return err
152+
}
151153
_, err = v6nd.pktSendF(buf.Bytes(), dstmac)
152154
return err
153155
}
@@ -213,11 +215,13 @@ func (v6nd *IPv6ND) probe(nei *Neighbor) (err error) {
213215
}
214216
respicmp6Layer.SetNetworkLayerForChecksum(iplayer)
215217
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
216-
gopacket.SerializeLayers(buf, opts,
218+
err = gopacket.SerializeLayers(buf, opts,
217219
iplayer,
218220
respicmp6Layer,
219221
resp)
220-
222+
if err != nil {
223+
return err
224+
}
221225
_, err = v6nd.pktSendF(buf.Bytes(), GetMulticastMACfromIPv6Addr(nei.IPAddr))
222226
return err
223227

@@ -249,7 +253,7 @@ func (v6nd *IPv6ND) houseKeeping(ctx context.Context) {
249253
}
250254
}
251255

252-
//pkt is should be an IP packet
256+
// pkt is should be an IP packet
253257
func (v6nd *IPv6ND) handleNDPkt(pkt gopacket.Packet) error {
254258
var mac net.HardwareAddr = nil
255259
var addr, target netip.Addr
@@ -315,7 +319,7 @@ func (v6nd *IPv6ND) handleNDPkt(pkt gopacket.Packet) error {
315319
return nil
316320
}
317321

318-
//Resolve resolve addr to mac based on local list, if not found and activeProbe is true, then send probe
322+
// Resolve resolve addr to mac based on local list, if not found and activeProbe is true, then send probe
319323
func (v6nd *IPv6ND) Resolve(addr netip.Addr) net.HardwareAddr {
320324
v6nd.listLock.RLock()
321325
if nei, ok := v6nd.neiList[addr]; ok {

pcaprelay.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ func (pconn *PcapConn) isTimeout(err error) bool {
6565
}
6666
}
6767

68+
// NewRawSocketRelayPcap creates a new RawSocketRelay instance using libpcap, bound to the interface ifname,
69+
// optionally with RelayOption functions.
70+
// This function will put the interface in promisc mode, which means it requires root privilage.
6871
func NewRawSocketRelayPcap(parentctx context.Context, ifname string, options ...RelayOption) (*RawSocketRelay, error) {
6972
conn, err := NewPcapConn(ifname)
7073
if err != nil {
7174
return nil, fmt.Errorf("failed to create rawsocketrelay,%w", err)
7275
}
73-
return NewRawSocketRelayWithRelayConn(parentctx, ifname, conn, options...)
76+
return newRawSocketRelayWithRelayConn(parentctx, ifname, conn, options...)
7477

7578
}

rawrelay.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ type rawRelayConn interface {
5252

5353
type LogFunc func(fmt string, a ...interface{})
5454

55-
// RawSocketRelay implements PacketRelay interface, using AF_PACKET socket
55+
// RawSocketRelay implements PacketRelay interface, using AF_PACKET socket or libpcap.
56+
// use NewRawSocketRelayPcap or NewRawSocketRelay create new instances.
5657
type RawSocketRelay struct {
5758
conn rawRelayConn
5859
toSendChan chan []byte
@@ -78,11 +79,11 @@ type RawSocketRelay struct {
7879
// RelayOption is a function use to provide customized option when creating RawSocketRelay
7980
type RelayOption func(*RawSocketRelay)
8081

81-
// NewRawSocketRelay creates a new RawSocketRelay instance,
82+
// newRawSocketRelay creates a new RawSocketRelay instance,
8283
// bound to the interface ifname,
8384
// optionally along with RelayOption functions.
8485
// This function will put the interface in promisc mode, which means it requires root privilage
85-
func NewRawSocketRelayWithRelayConn(parentctx context.Context, ifname string, conn rawRelayConn, options ...RelayOption) (*RawSocketRelay, error) {
86+
func newRawSocketRelayWithRelayConn(parentctx context.Context, ifname string, conn rawRelayConn, options ...RelayOption) (*RawSocketRelay, error) {
8687
r := &RawSocketRelay{}
8788
var err error
8889
r.ifName = ifname
@@ -198,6 +199,7 @@ func WithDefaultReceival(mirroring bool) RelayOption {
198199
}
199200
}
200201

202+
// Type returns rsr's type
201203
func (rsr *RawSocketRelay) Type() RelayType {
202204
return rsr.conn.relayType()
203205
}

xdpconn_linux.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ func (s *xdpSock) recv(ctx context.Context) {
246246
for i := 0; i < len(rxDescs); i++ {
247247
pktData := make([]byte, len(s.sock.GetFrame(rxDescs[i])))
248248
copy(pktData, s.sock.GetFrame(rxDescs[i]))
249-
if s.relay.rxh != nil {
250-
s.relay.rxh(pktData, s.qid)
251-
}
249+
// if s.relay.rxh != nil {
250+
// s.relay.rxh(pktData, s.qid)
251+
// }
252252
handleRcvPkt(UnknownRelay, pktData, s.relay.stats, logf,
253253
s.relay.recvList, s.relay.mirrorToDefault,
254254
s.relay.defaultRecvChan, s.relay.multicastList, nil)
@@ -284,9 +284,9 @@ type XDPRelay struct {
284284
defaultRecvChan chan *RelayReceival
285285
mirrorToDefault bool
286286
recvBytesChan chan []byte
287-
rxh, txh XDPSocketPktHandler
288-
sendingMode XDPSendingMode
289-
recvEtypes []uint16
287+
// rxh, txh XDPSocketPktHandler
288+
sendingMode XDPSendingMode
289+
recvEtypes []uint16
290290
}
291291

292292
// XDPRelayOption could be used in NewXDPRelay to customize XDPRelay upon creation
@@ -400,18 +400,18 @@ func WithXDPExtProg(fname, prog, qmap, xskmap, etypemap string) XDPRelayOption {
400400
}
401401

402402
// WithXDPRXPktHandler sets h as the rx packet handler
403-
func WithXDPRXPktHandler(h XDPSocketPktHandler) XDPRelayOption {
404-
return func(relay *XDPRelay) {
405-
relay.rxh = h
406-
}
407-
}
403+
// func WithXDPRXPktHandler(h XDPSocketPktHandler) XDPRelayOption {
404+
// return func(relay *XDPRelay) {
405+
// relay.rxh = h
406+
// }
407+
// }
408408

409409
// WithXDPTXPktHandler sets h as the tx packet handler
410-
func WithXDPTXPktHandler(h XDPSocketPktHandler) XDPRelayOption {
411-
return func(relay *XDPRelay) {
412-
relay.txh = h
413-
}
414-
}
410+
// func WithXDPTXPktHandler(h XDPSocketPktHandler) XDPRelayOption {
411+
// return func(relay *XDPRelay) {
412+
// relay.txh = h
413+
// }
414+
// }
415415

416416
const (
417417
// DefaultXDPUMEMNumOfTrunk is the default number of UMEM trunks
@@ -611,7 +611,7 @@ func SetIfVLANOffloading(ifname string, enable bool) error {
611611
return etool.Change(ifname, vlanoffloadingsetting)
612612
}
613613

614-
const builtinProgFileName = "xdpethfilter_kern.o"
614+
// const builtinProgFileName = "xdpethfilter_kern.o"
615615

616616
func loadEBPFProgViaReader(r io.ReaderAt, funcname, qidmapname, xskmapname, ethertypemap string) (*xdp.Program, *ebpf.Map, error) {
617617
prog := new(xdp.Program)
@@ -633,7 +633,7 @@ func loadEBPFProgViaReader(r io.ReaderAt, funcname, qidmapname, xskmapname, ethe
633633
if prog.Sockets, ok = col.Maps[xskmapname]; !ok {
634634
return nil, nil, fmt.Errorf("can't find a socket map named %v", xskmapname)
635635
}
636-
var elist *ebpf.Map = nil
636+
var elist *ebpf.Map
637637
if elist, ok = col.Maps[ethertypemap]; !ok {
638638
return nil, nil, fmt.Errorf("can't find a socket map named %v", xskmapname)
639639
}

0 commit comments

Comments
 (0)