Skip to content
Open
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
158 changes: 158 additions & 0 deletions ovs/openflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
)

var (
Expand Down Expand Up @@ -69,6 +72,71 @@ type flowDirective struct {
flow string
}

// PortMapping contains the ofport number and MAC address for an interface.
type PortMapping struct {
// ofPort specifies the OpenFlow port number.
ofPort int

// macAddress specifies the MAC address of the interface.
macAddress string
}

// UnmarshalText unmarshals a PortMapping from textual form as output by
// 'ovs-ofctl show':
//
// 7(interface1): addr:fe:4f:76:09:88:2b
func (p *PortMapping) UnmarshalText(b []byte) error {
// Make a copy per documentation for encoding.TextUnmarshaler.
s := strings.TrimSpace(string(b))

// Expected format: " 7(interface1): addr:fe:4f:76:09:88:2b"
// Find the opening parenthesis
openParen := strings.IndexByte(s, '(')
if openParen == -1 {
return fmt.Errorf("invalid port mapping format")
}

// Find the closing parenthesis in the full string
closeParen := strings.IndexByte(s, ')')
if closeParen == -1 || closeParen <= openParen {
return fmt.Errorf("invalid port mapping format")
}

// Extract ofport number (before opening parenthesis)
ofportStr := strings.TrimSpace(s[:openParen])
ofport, err := strconv.Atoi(ofportStr)
if err != nil {
return fmt.Errorf("invalid ofport number: %w", err)
}

// Validate ofport is in valid OpenFlow port range [0, 65535]
if ofport < 0 || ofport > 65535 {
return fmt.Errorf("ofport %d out of valid range [0, 65535]", ofport)
}

// Find "addr:" after the closing parenthesis in the full string
addrPrefix := ": addr:"
addrIdx := strings.Index(s, addrPrefix)
if addrIdx == -1 || addrIdx <= closeParen {
return fmt.Errorf("invalid port mapping format")
}
addrIdx += len(addrPrefix)

// Extract MAC address (after "addr:")
macAddress := strings.TrimSpace(s[addrIdx:])

// Validate MAC address format
if _, err := net.ParseMAC(macAddress); err != nil {
return fmt.Errorf("invalid MAC address %q: %w", macAddress, err)
}

p.ofPort = ofport
// Note: interface name is not stored in PortMapping, it's used as map key
p.macAddress = macAddress

return nil
}

// Possible flowDirective directive values.
const (
dirAdd = "add"
Expand Down Expand Up @@ -324,6 +392,59 @@ func (o *OpenFlowService) DumpAggregate(bridge string, flow *MatchFlow) (*FlowSt
return stats, nil
}

// DumpPortMapping retrieves port mapping (ofport and MAC address) for a
// specific interface from the specified bridge.
func (o *OpenFlowService) DumpPortMapping(bridge string, interfaceName string) (*PortMapping, error) {
mappings, err := o.DumpPortMappings(bridge)
if err != nil {
return nil, err
}

mapping, ok := mappings[interfaceName]
if !ok {
return nil, fmt.Errorf("interface %q not found", interfaceName)
}

return mapping, nil
}

// DumpPortMappings retrieves port mappings (ofport and MAC address) for all
// interfaces from the specified bridge. The output is parsed from 'ovs-ofctl show' command.
func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortMapping, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to add one variant of this function in addition to this function. It should also be possible using another function to return the PortMapping for a single interface index provided as an input. Also, see my comment in the test section - you can modify the test accordingly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

args := []string{"show", bridge}
args = append(args, o.c.ofctlFlags...)

out, err := o.exec(args...)
if err != nil {
return nil, err
}

mappings := make(map[string]*PortMapping)
err = parseEachPort(out, showPrefix, func(line []byte) error {
// Parse the PortMapping - UnmarshalText will validate format
pm := new(PortMapping)
if err := pm.UnmarshalText(line); err != nil {
// Skip malformed lines
return nil
}

// Extract interface name from the validated line for map key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move parsing of interface name to UnmarshalText as well. You may need to declare another structure to accomplish this e.g.

type PortAttr struct {
	// ofPort specifies the OpenFlow port number.
	ofPort int

	// macAddress specifies the MAC address of the interface.
	macAddress string
}
type PortMapping struct {
	portAttr PortAttr
	ifName string
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

s := string(line)
openParen := strings.IndexByte(s, '(')
closeParen := strings.IndexByte(s, ')')
interfaceName := s[openParen+1 : closeParen]

mappings[interfaceName] = pm
return nil
})

if err != nil {
return nil, fmt.Errorf("failed to parse port mappings: %w", err)
}

return mappings, nil
}

var (
// dumpPortsPrefix is a sentinel value returned at the beginning of
// the output from 'ovs-ofctl dump-ports'.
Expand All @@ -343,6 +464,10 @@ var (
// dumpAggregatePrefix is a sentinel value returned at the beginning of
// the output from "ovs-ofctl dump-aggregate"
//dumpAggregatePrefix = []byte("NXST_AGGREGATE reply")

// showPrefix is a sentinel value returned at the beginning of
// the output from 'ovs-ofctl show'.
showPrefix = []byte("OFPT_FEATURES_REPLY")
)

// dumpPorts calls 'ovs-ofctl dump-ports' with the specified arguments and
Expand Down Expand Up @@ -497,6 +622,39 @@ func parseEach(in []byte, prefix []byte, fn func(b []byte) error) error {
return scanner.Err()
}

// parseEachPort parses ovs-ofctl show output from the input buffer, ensuring it has the
// specified prefix, and invoking the input function on each line scanned.
func parseEachPort(in []byte, prefix []byte, fn func(b []byte) error) error {
// Handle empty input - return nil to allow empty mappings (matches test expectations)
if len(in) == 0 {
return nil
}

// First line must not be empty
scanner := bufio.NewScanner(bytes.NewReader(in))
scanner.Split(bufio.ScanLines)
if !scanner.Scan() {
return io.ErrUnexpectedEOF
}

// First line must contain the prefix returned by OVS
if !bytes.Contains(scanner.Bytes(), prefix) {
return io.ErrUnexpectedEOF
}

// Scan every line to retrieve information needed to unmarshal
// a single PortMapping struct.
for scanner.Scan() {
b := make([]byte, len(scanner.Bytes()))
copy(b, scanner.Bytes())
if err := fn(b); err != nil {
return err
}
}

return scanner.Err()
}

// exec executes an ExecFunc using 'ovs-ofctl'.
func (o *OpenFlowService) exec(args ...string) ([]byte, error) {
return o.c.exec("ovs-ofctl", args...)
Expand Down
Loading
Loading