-
Notifications
You must be signed in to change notification settings - Fork 109
GATEWAYS-4422 - Add DumpPortMappings API to retrieve ofport and MAC address for tap interfaces #141
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
Open
yrkurapati
wants to merge
3
commits into
digitalocean:master
Choose a base branch
from
yrkurapati:GATEWAYS-4422-implement-go-openvswitch-api-for-ofport-interface-mapping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,9 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -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" | ||
|
|
@@ -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) { | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'. | ||
|
|
@@ -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 | ||
|
|
@@ -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...) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.