Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gofmt files and rename linux-only files to _linux to remove build-tag comments #31

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions constants.go → constants_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build linux

package ipvs

const (
Expand Down
3 changes: 0 additions & 3 deletions ipvs.go → ipvs_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build linux

package ipvs

import (
Expand Down Expand Up @@ -181,7 +179,6 @@ func (i *Handle) GetDestinations(s *Service) ([]*Destination, error) {

// GetService gets details of a specific IPVS services, useful in updating statisics etc.,
func (i *Handle) GetService(s *Service) (*Service, error) {

res, err := i.doGetServicesCmd(s)
if err != nil {
return nil, err
Expand Down
10 changes: 0 additions & 10 deletions ipvs_test.go → ipvs_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build linux
// +build linux

package ipvs

import (
Expand Down Expand Up @@ -47,7 +44,6 @@ var (
)

func lookupFwMethod(fwMethod uint32) string {

switch fwMethod {
case ConnectionFlagMasq:
return fwdMethodStrings[0]
Expand Down Expand Up @@ -79,19 +75,16 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check
switch checkPresent {
case true: // The test expects the service to be present
if !dstFound {

t.Fatalf("Did not find the service %s in ipvs output", d.Address.String())
}
case false: // The test expects that the service should not be present
if dstFound {
t.Fatalf("Did not find the destination %s fwdMethod %s in ipvs output", d.Address.String(), lookupFwMethod(d.ConnectionFlags))
}
}

}

func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {

svcArray, err := i.GetServices()
if err != nil {
t.Fatalf("Failed to get service; %v", err)
Expand All @@ -100,7 +93,6 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {
var svcFound bool

for _, svc := range svcArray {

if svc.Protocol == s.Protocol && svc.Address.String() == s.Address.String() && svc.Port == s.Port {
svcFound = true
break
Expand All @@ -110,15 +102,13 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) {
switch checkPresent {
case true: // The test expects the service to be present
if !svcFound {

t.Fatalf("Did not find the service %s in ipvs output", s.Address.String())
}
case false: // The test expects that the service should not be present
if svcFound {
t.Fatalf("Did not expect the service %s in ipvs output", s.Address.String())
}
}

}

func TestGetFamily(t *testing.T) {
Expand Down
34 changes: 14 additions & 20 deletions netlink.go → netlink_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build linux

package ipvs

import (
Expand Down Expand Up @@ -124,8 +122,8 @@ func (i *Handle) doCmdwithResponse(s *Service, d *Destination, cmd uint8) ([][]b
req.Seq = atomic.AddUint32(&i.seq, 1)

if s == nil {
req.Flags |= syscall.NLM_F_DUMP //Flag to dump all messages
req.AddData(nl.NewRtAttr(ipvsCmdAttrService, nil)) //Add a dummy attribute
req.Flags |= syscall.NLM_F_DUMP // Flag to dump all messages
req.AddData(nl.NewRtAttr(ipvsCmdAttrService, nil)) // Add a dummy attribute
} else {
req.AddData(fillService(s))
}
Expand All @@ -134,7 +132,6 @@ func (i *Handle) doCmdwithResponse(s *Service, d *Destination, cmd uint8) ([][]b
if cmd == ipvsCmdGetDest {
req.Flags |= syscall.NLM_F_DUMP
}

} else {
req.AddData(fillDestination(d))
}
Expand Down Expand Up @@ -259,7 +256,6 @@ done:
}

func parseIP(ip []byte, family uint16) (net.IP, error) {

var resIP net.IP

switch family {
Expand All @@ -276,7 +272,6 @@ func parseIP(ip []byte, family uint16) (net.IP, error) {

// parseStats
func assembleStats(msg []byte) (SvcStats, error) {

var s SvcStats

attrs, err := nl.ParseRouteAttr(msg)
Expand Down Expand Up @@ -314,7 +309,6 @@ func assembleStats(msg []byte) (SvcStats, error) {

// assembleService assembles a services back from a hain of netlink attributes
func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {

var s Service
var addressBytes []byte

Expand Down Expand Up @@ -366,10 +360,9 @@ func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {

// parseService given a ipvs netlink response this function will respond with a valid service entry, an error otherwise
func (i *Handle) parseService(msg []byte) (*Service, error) {

var s *Service

//Remove General header for this message and parse the NetLink message
// Remove General header for this message and parse the NetLink message
hdr := deserializeGenlMsg(msg)
NetLinkAttrs, err := nl.ParseRouteAttr(msg[hdr.Len():])
if err != nil {
Expand All @@ -379,13 +372,13 @@ func (i *Handle) parseService(msg []byte) (*Service, error) {
return nil, fmt.Errorf("error no valid netlink message found while parsing service record")
}

//Now Parse and get IPVS related attributes messages packed in this message.
// Now Parse and get IPVS related attributes messages packed in this message.
ipvsAttrs, err := nl.ParseRouteAttr(NetLinkAttrs[0].Value)
if err != nil {
return nil, err
}

//Assemble all the IPVS related attribute messages and create a service record
// Assemble all the IPVS related attribute messages and create a service record
s, err = assembleService(ipvsAttrs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -422,7 +415,6 @@ func (i *Handle) doCmdWithoutAttr(cmd uint8) ([][]byte, error) {
}

func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) {

var d Destination
var addressBytes []byte

Expand Down Expand Up @@ -486,9 +478,12 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error)

// getIPFamily parses the IP family based on raw data from netlink.
// For AF_INET, netlink will set the first 4 bytes with trailing zeros
// 10.0.0.1 -> [10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
//
// 10.0.0.1 -> [10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
//
// For AF_INET6, the full 16 byte array is used:
// 2001:db8:3c4d:15::1a00 -> [32 1 13 184 60 77 0 21 0 0 0 0 0 0 26 0]
//
// 2001:db8:3c4d:15::1a00 -> [32 1 13 184 60 77 0 21 0 0 0 0 0 0 26 0]
func getIPFamily(address []byte) (uint16, error) {
if len(address) == 4 {
return syscall.AF_INET, nil
Expand Down Expand Up @@ -519,7 +514,7 @@ func isZeros(b []byte) bool {
func (i *Handle) parseDestination(msg []byte) (*Destination, error) {
var dst *Destination

//Remove General header for this message
// Remove General header for this message
hdr := deserializeGenlMsg(msg)
NetLinkAttrs, err := nl.ParseRouteAttr(msg[hdr.Len():])
if err != nil {
Expand All @@ -529,13 +524,13 @@ func (i *Handle) parseDestination(msg []byte) (*Destination, error) {
return nil, fmt.Errorf("error no valid netlink message found while parsing destination record")
}

//Now Parse and get IPVS related attributes messages packed in this message.
// Now Parse and get IPVS related attributes messages packed in this message.
ipvsAttrs, err := nl.ParseRouteAttr(NetLinkAttrs[0].Value)
if err != nil {
return nil, err
}

//Assemble netlink attributes and create a Destination record
// Assemble netlink attributes and create a Destination record
dst, err = assembleDestination(ipvsAttrs)
if err != nil {
return nil, err
Expand All @@ -546,7 +541,6 @@ func (i *Handle) parseDestination(msg []byte) (*Destination, error) {

// doGetDestinationsCmd a wrapper function to be used by GetDestinations and GetDestination(d) apis
func (i *Handle) doGetDestinationsCmd(s *Service, d *Destination) ([]*Destination, error) {

var res []*Destination

msgs, err := i.doCmdwithResponse(s, d, ipvsCmdGetDest)
Expand All @@ -568,7 +562,7 @@ func (i *Handle) doGetDestinationsCmd(s *Service, d *Destination) ([]*Destinatio
func (i *Handle) parseConfig(msg []byte) (*Config, error) {
var c Config

//Remove General header for this message
// Remove General header for this message
hdr := deserializeGenlMsg(msg)
attrs, err := nl.ParseRouteAttr(msg[hdr.Len():])
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions netlink_test.go → netlink_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build linux
// +build linux

package ipvs

import (
Expand Down