Skip to content

Commit

Permalink
rename variables go idiomatically
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <leon.gross@9elements.com>
  • Loading branch information
leongross committed Jun 14, 2024
1 parent 6583c81 commit 77b9dac
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
87 changes: 43 additions & 44 deletions pkg/brctl/brctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func Addbr(name string) error {

// Delbr deletes a bridge with the name provided.
func Delbr(name string) error {
brctl_socket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
brctlSocket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
if err != nil {
return fmt.Errorf("unix.Socket: %w", err)

Check warning on line 38 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L38

Added line #L38 was not covered by tests
}

if _, err := executeIoctlStr(brctl_socket, unix.SIOCBRDELBR, name); err != nil {
if _, err := executeIoctlStr(brctlSocket, unix.SIOCBRDELBR, name); err != nil {
return fmt.Errorf("executeIoctlStr: %w", err)
}

Expand All @@ -47,7 +47,7 @@ func Delbr(name string) error {

// Addif adds an interface to the bridge provided
func Addif(bridge string, iface string) error {
brctl_socket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
brctlSocket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
if err != nil {
return fmt.Errorf("unix.Socket: %w", err)

Check warning on line 52 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L52

Added line #L52 was not covered by tests
}
Expand All @@ -57,13 +57,13 @@ func Addif(bridge string, iface string) error {
return fmt.Errorf("unix.NewIfreq: %w", err)

Check warning on line 57 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L57

Added line #L57 was not covered by tests
}

if_index, err := getIndexFromInterfaceName(iface)
ifIndex, err := getIndexFromInterfaceName(iface)
if err != nil {
return fmt.Errorf("getIndexFromInterfaceName: %w", err)

Check warning on line 62 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L62

Added line #L62 was not covered by tests
}
ifr.SetUint32(uint32(if_index))
ifr.SetUint32(uint32(ifIndex))

if err := unix.IoctlIfreq(brctl_socket, unix.SIOCBRADDIF, ifr); err != nil {
if err := unix.IoctlIfreq(brctlSocket, unix.SIOCBRADDIF, ifr); err != nil {
return fmt.Errorf("unix.IoctlIfreq: %w", err)

Check warning on line 67 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L67

Added line #L67 was not covered by tests
}

Expand All @@ -72,7 +72,7 @@ func Addif(bridge string, iface string) error {

// Delif deleted a given interface from the bridge
func Delif(bridge string, iface string) error {
brctl_socket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
brctlSocket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
if err != nil {
return fmt.Errorf("unix.Socket: %w", err)

Check warning on line 77 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L77

Added line #L77 was not covered by tests
}
Expand All @@ -82,13 +82,13 @@ func Delif(bridge string, iface string) error {
return fmt.Errorf("unix.NewIfreq: %w", err)

Check warning on line 82 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L82

Added line #L82 was not covered by tests
}

if_index, err := getIndexFromInterfaceName(iface)
if err != nil || if_index == 0 {
ifIndex, err := getIndexFromInterfaceName(iface)
if err != nil || ifIndex == 0 {
return fmt.Errorf("getIndexFromInterfaceName: %w", err)

Check warning on line 87 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L87

Added line #L87 was not covered by tests
}
ifr.SetUint32(uint32(if_index))
ifr.SetUint32(uint32(ifIndex))

if err := unix.IoctlIfreq(brctl_socket, unix.SIOCBRDELIF, ifr); err != nil {
if err := unix.IoctlIfreq(brctlSocket, unix.SIOCBRDELIF, ifr); err != nil {
return fmt.Errorf("unix.IoctlIfreq: %w", err)

Check warning on line 92 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L92

Added line #L92 was not covered by tests
}

Expand All @@ -98,24 +98,24 @@ func Delif(bridge string, iface string) error {
// All bridges are in the virtfs under /sys/class/net/<name>/bridge/<item>, read info from there
// Update this function if BridgeInfo struct changes
func getBridgeInfo(name string) (BridgeInfo, error) {
base_path := path.Join(BRCTL_SYS_NET, name, "bridge")
bridge_id, err := os.ReadFile(path.Join(base_path, "bridge_id"))
basePath := path.Join(BRCTL_SYS_NET, name, "bridge")
bridgeID, err := os.ReadFile(path.Join(basePath, BRCTL_BRIDGEID))
if err != nil {
return BridgeInfo{}, fmt.Errorf("os.ReadFile: %w", err)

Check warning on line 104 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L104

Added line #L104 was not covered by tests
}

stp_enabled, err := os.ReadFile(path.Join(base_path, "stp_state"))
stpEnabledRaw, err := os.ReadFile(path.Join(basePath, BRCTL_STP_STATE))
if err != nil {
return BridgeInfo{}, fmt.Errorf("os.ReadFile: %w", err)

Check warning on line 109 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L109

Added line #L109 was not covered by tests
}

stp_enabled_bool, err := strconv.ParseBool(strings.TrimSuffix(string(stp_enabled), "\n"))
stpEnabled, err := strconv.ParseBool(strings.TrimSuffix(string(stpEnabledRaw), "\n"))
if err != nil {
return BridgeInfo{}, fmt.Errorf("strconv.ParseBool: %w", err)

Check warning on line 114 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L114

Added line #L114 was not covered by tests
}

// get interfaceDir from sysfs
interfaceDir, err := os.ReadDir(path.Join(BRCTL_SYS_NET, name, "brif"))
interfaceDir, err := os.ReadDir(path.Join(BRCTL_SYS_NET, name, BRCTL_BRIDGE_INTERFACE))
if err != nil {
return BridgeInfo{}, fmt.Errorf("os.ReadDir: %w", err)

Check warning on line 120 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L120

Added line #L120 was not covered by tests
}
Expand All @@ -127,8 +127,8 @@ func getBridgeInfo(name string) (BridgeInfo, error) {

return BridgeInfo{
Name: name,
BridgeID: strings.TrimSuffix(string(bridge_id), "\n"),
StpState: stp_enabled_bool,
BridgeID: strings.TrimSuffix(string(bridgeID), "\n"),
StpState: stpEnabled,
Interfaces: interfaces,
}, nil

Expand Down Expand Up @@ -157,22 +157,21 @@ func showBridge(name string, out io.Writer) {
// 09-10: is_local
// 11-15: timeval (ignored for now)
func Showmacs(bridge string, out io.Writer) error {

Check warning on line 159 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L159

Added line #L159 was not covered by tests

// parse sysf into 0x10 byte chunks
brforward, err := os.ReadFile(path.Join(BRCTL_SYS_NET, bridge, "brforward"))
brforward, err := os.ReadFile(path.Join(BRCTL_SYS_NET, bridge, BRCTL_BRFORWARD))
if err != nil {
return fmt.Errorf("Readfile(%q): %w", path.Join(BRCTL_SYS_NET, bridge, "brforward"), err)
return fmt.Errorf("Readfile(%q): %w", path.Join(BRCTL_SYS_NET, bridge, BRCTL_BRFORWARD), err)

Check warning on line 163 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L161-L163

Added lines #L161 - L163 were not covered by tests
}

fmt.Fprintf(out, "port no\tmac addr\t\tis_local?\n")

Check warning on line 166 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L166

Added line #L166 was not covered by tests

for i := 0; i < len(brforward); i += 0x10 {
chunk := brforward[i : i+0x10]
mac := chunk[0:6]
port_no := uint16(binary.BigEndian.Uint16(chunk[6:8]))
is_local := uint8(chunk[9]) != 0
portNo := uint16(binary.BigEndian.Uint16(chunk[6:8]))
isLocal := uint8(chunk[9]) != 0

Check warning on line 172 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L168-L172

Added lines #L168 - L172 were not covered by tests

fmt.Fprintf(out, "%3d\t%2x:%2x:%2x:%2x:%2x:%2x\t%v\n", port_no, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], is_local)
fmt.Fprintf(out, "%3d\t%2x:%2x:%2x:%2x:%2x:%2x\t%v\n", portNo, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], isLocal)

Check warning on line 174 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L174

Added line #L174 was not covered by tests
}

return nil

Check warning on line 177 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L177

Added line #L177 was not covered by tests
Expand Down Expand Up @@ -206,12 +205,12 @@ func Show(out io.Writer, names ...string) error {
// After <time> seconds of not having seen a frame coming from a certain address,
// the bridge will time out (delete) that address from the Forwarding DataBase (fdb).
func Setageingtime(name string, time string) error {
ageing_time, err := stringToJiffies(time)
ageingTime, err := stringToJiffies(time)
if err != nil {
return fmt.Errorf("stringToJiffies(%q) = %w", time, err)

Check warning on line 210 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L210

Added line #L210 was not covered by tests
}

if err = setBridgeValue(name, BRCTL_AGEING_TIME, []byte(strconv.Itoa(ageing_time)), uint64(BRCTL_SET_AEGING_TIME)); err != nil {
if err = setBridgeValue(name, BRCTL_AGEING_TIME, []byte(strconv.Itoa(ageingTime)), uint64(BRCTL_SET_AEGING_TIME)); err != nil {
return fmt.Errorf("setBridgeValue: %w", err)

Check warning on line 214 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L214

Added line #L214 was not covered by tests
}
return nil
Expand All @@ -223,14 +222,14 @@ func Setageingtime(name string, time string) error {
// > If <state> is "on" or "yes" the STP will be turned on, otherwise it will be turned off
// So this is actually the described behavior, not checking for "off" and "no"
func Stp(bridge string, state string) error {
var stp_state int
var stpState int
if state == "on" || state == "yes" {
stp_state = 1
stpState = 1
} else {
stp_state = 0
stpState = 0

Check warning on line 229 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L229

Added line #L229 was not covered by tests
}

if err := setBridgeValue(bridge, BRCTL_STP_STATE, []byte(strconv.Itoa(stp_state)), uint64(BRCTL_SET_BRIDGE_PRIORITY)); err != nil {
if err := setBridgeValue(bridge, BRCTL_STP_STATE, []byte(strconv.Itoa(stpState)), uint64(BRCTL_SET_BRIDGE_PRIORITY)); err != nil {
return fmt.Errorf("setBridgeValue: %w", err)

Check warning on line 233 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L233

Added line #L233 was not covered by tests
}

Expand All @@ -256,12 +255,12 @@ func Setbridgeprio(bridge string, bridgePriority string) error {

// Setfd sets the bridge's 'bridge forward delay' to <time> seconds.
func Setfd(bridge string, time string) error {
forward_delay, err := stringToJiffies(time)
forwardDelay, err := stringToJiffies(time)
if err != nil {
return fmt.Errorf("stringToJiffies(%q) = %w", time, err)

Check warning on line 260 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L260

Added line #L260 was not covered by tests
}

if err := setBridgeValue(bridge, BRCTL_FORWARD_DELAY, []byte(strconv.Itoa(forward_delay)), 0); err != nil {
if err := setBridgeValue(bridge, BRCTL_FORWARD_DELAY, []byte(strconv.Itoa(forwardDelay)), 0); err != nil {
return fmt.Errorf("setBridgeValue: %w", err)

Check warning on line 264 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L264

Added line #L264 was not covered by tests
}

Expand All @@ -270,12 +269,12 @@ func Setfd(bridge string, time string) error {

// Sethello sets the bridge's 'bridge hello time' to <time> seconds.
func Sethello(bridge string, time string) error {
hello_time, err := stringToJiffies(time)
helloTime, err := stringToJiffies(time)
if err != nil {
return fmt.Errorf("stringToJiffies(%q) = %w", time, err)

Check warning on line 274 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L274

Added line #L274 was not covered by tests
}

if err := setBridgeValue(bridge, BRCTL_HELLO_TIME, []byte(strconv.Itoa(hello_time)), 0); err != nil {
if err := setBridgeValue(bridge, BRCTL_HELLO_TIME, []byte(strconv.Itoa(helloTime)), 0); err != nil {
return fmt.Errorf("setBridgeValue: %w", err)

Check warning on line 278 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L278

Added line #L278 was not covered by tests
}

Expand All @@ -284,12 +283,12 @@ func Sethello(bridge string, time string) error {

// Setmaxage sets the bridge's 'maximum message age' to <time> seconds.
func Setmaxage(bridge string, time string) error {
max_age, err := stringToJiffies(time)
maxAge, err := stringToJiffies(time)
if err != nil {
return fmt.Errorf("stringToJiffies(%q) = %w", time, err)

Check warning on line 288 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L285-L288

Added lines #L285 - L288 were not covered by tests
}

if err := setBridgeValue(bridge, BRCTL_MAX_AGE, []byte(strconv.Itoa(max_age)), 0); err != nil {
if err := setBridgeValue(bridge, BRCTL_MAX_AGE, []byte(strconv.Itoa(maxAge)), 0); err != nil {
return fmt.Errorf("setBridgeValue: %w", err)

Check warning on line 292 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L291-L292

Added lines #L291 - L292 were not covered by tests
}

Expand All @@ -298,12 +297,12 @@ func Setmaxage(bridge string, time string) error {

// Setpathcost sets the port cost of the port <port> to <cost>. This is a dimensionless metric.
func Setpathcost(bridge string, port string, cost string) error {
path_cost, err := strconv.ParseUint(cost, 10, 64)
pathCost, err := strconv.ParseUint(cost, 10, 64)
if err != nil {
return err

Check warning on line 302 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L302

Added line #L302 was not covered by tests
}

err = setPortBrportValue(port, BRCTL_PATH_COST, append([]byte(strconv.FormatUint(path_cost, 10)), BRCTL_SYS_SUFFIX))
err = setPortBrportValue(port, BRCTL_PATH_COST, append([]byte(strconv.FormatUint(pathCost, 10)), BRCTL_SYS_SUFFIX))
if err != nil {
return fmt.Errorf("setPortBrportValue: %w", err)

Check warning on line 307 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L307

Added line #L307 was not covered by tests
}
Expand All @@ -315,24 +314,24 @@ func Setpathcost(bridge string, port string, cost string) error {
// The priority value is an unsigned 8-bit quantity (a number between 0 and 255),
// and has no dimension. This metric is used in the designated port and root port selection algorithms.
func Setportprio(bridge string, port string, prio string) error {
port_priority, err := strconv.Atoi(prio)
portPriority, err := strconv.Atoi(prio)
if err != nil {
return err

Check warning on line 319 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L319

Added line #L319 was not covered by tests
}

return setPortBrportValue(port, BRCTL_PRIORITY, []byte(strconv.Itoa(port_priority)))
return setPortBrportValue(port, BRCTL_PRIORITY, []byte(strconv.Itoa(portPriority)))
}

// Hairpin sets the hairpin mode of the <port> attached to <bridge>
func Hairpin(bridge string, port string, hairpinmode string) error {
var hairpin_mode string
var hairpinMode string
if hairpinmode == "on" {
hairpin_mode = "1"
hairpinMode = "1"
} else {
hairpin_mode = "0"
hairpinMode = "0"

Check warning on line 331 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L331

Added line #L331 was not covered by tests
}

if err := setPortBrportValue(port, BRCTL_HAIRPIN, []byte(hairpin_mode)); err != nil {
if err := setPortBrportValue(port, BRCTL_HAIRPIN, []byte(hairpinMode)); err != nil {
return fmt.Errorf("setPortBrportValue: %w", err)

Check warning on line 335 in pkg/brctl/brctl.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/brctl.go#L335

Added line #L335 was not covered by tests
}

Expand Down
21 changes: 12 additions & 9 deletions pkg/brctl/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ const (
)

const (
BRCTL_AGEING_TIME = "ageing_time"
BRCTL_STP_STATE = "stp_state"
BRCTL_BRIDGE_PRIO = "priority"
BRCTL_FORWARD_DELAY = "forward_delay"
BRCTL_HELLO_TIME = "hello_time"
BRCTL_MAX_AGE = "max_age"
BRCTL_PATH_COST = "path_cost"
BRCTL_PRIORITY = "priority"
BRCTL_HAIRPIN = "hairpin_mode"
BRCTL_AGEING_TIME = "ageing_time"
BRCTL_STP_STATE = "stp_state"
BRCTL_BRIDGE_PRIO = "priority"
BRCTL_FORWARD_DELAY = "forward_delay"
BRCTL_HELLO_TIME = "hello_time"
BRCTL_MAX_AGE = "max_age"
BRCTL_PATH_COST = "path_cost"
BRCTL_PRIORITY = "priority"
BRCTL_HAIRPIN = "hairpin_mode"
BRCTL_BRFORWARD = "brforward"
BRCTL_BRIDGEID = "bridge_id"
BRCTL_BRIDGE_INTERFACE = "brif"
)
14 changes: 7 additions & 7 deletions pkg/brctl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func sysconfhz() (int, error) {
}

func executeIoctlStr(fd int, req uint, raw string) (int, error) {
local_bytes := append([]byte(raw), 0)
_, _, errno := syscall.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(req), uintptr(unsafe.Pointer(&local_bytes[0])))
localBytes := append([]byte(raw), 0)
_, _, errno := syscall.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(req), uintptr(unsafe.Pointer(&localBytes[0])))
if errno != 0 {
return 0, fmt.Errorf("syscall.Syscall: %w", errno)
}
Expand All @@ -55,22 +55,22 @@ func getIndexFromInterfaceName(ifname string) (int, error) {
return 0, err

Check warning on line 55 in pkg/brctl/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/util.go#L55

Added line #L55 was not covered by tests
}

brctl_socket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
brctlSocket, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
if err != nil {
return 0, err

Check warning on line 60 in pkg/brctl/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/util.go#L60

Added line #L60 was not covered by tests
}

err = unix.IoctlIfreq(brctl_socket, unix.SIOCGIFINDEX, ifreq)
err = unix.IoctlIfreq(brctlSocket, unix.SIOCGIFINDEX, ifreq)
if err != nil {
return 0, err

Check warning on line 65 in pkg/brctl/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/util.go#L65

Added line #L65 was not covered by tests
}

ifr_ifindex := ifreq.Uint32()
if ifr_ifindex == 0 {
ifrIfindex := ifreq.Uint32()
if ifrIfindex == 0 {
return 0, fmt.Errorf("interface %s not found", ifname)

Check warning on line 70 in pkg/brctl/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/brctl/util.go#L70

Added line #L70 was not covered by tests
}

return int(ifr_ifindex), nil
return int(ifrIfindex), nil
}

// set values for the bridge
Expand Down

0 comments on commit 77b9dac

Please sign in to comment.