-
Notifications
You must be signed in to change notification settings - Fork 260
Adding network monitor capabilities for prerouting and postrouting ebrules #527
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4e42959
About to take debugging comments out
pjohnst5 b5950f9
Removed debugging statements and tested again
pjohnst5 4929260
Removed more debugging statements and unused method
pjohnst5 fa90195
Made changes suggested by Tamilmani, tested again.
pjohnst5 99a5c5a
Made spacing changes suggested by Tamilmani
pjohnst5 19e67dd
Triggering new tests to run
pjohnst5 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
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FROM ubuntu:latest | ||
| RUN apt -y update | ||
| RUN apt-get -y upgrade | ||
| RUN apt install -y ebtables | ||
| RUN apt install -y net-tools | ||
| COPY networkmonitor /usr/bin/networkmonitor | ||
| CMD ["/usr/bin/networkmonitor"] | ||
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package cnms | ||
|
|
||
| type NetworkMonitor struct { | ||
| AddRulesToBeValidated map[string]int | ||
| DeleteRulesToBeValidated map[string]int | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package cnms | ||
|
|
||
| import ( | ||
| "github.com/Azure/azure-container-networking/ebtables" | ||
| "github.com/Azure/azure-container-networking/log" | ||
| ) | ||
|
|
||
pjohnst5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // deleteRulesNotExistInMap deletes rules from nat Ebtable if rule was not in stateRules after a certain number of iterations. | ||
| func (networkMonitor *NetworkMonitor) deleteRulesNotExistInMap(chainRules map[string]string, stateRules map[string]string) { | ||
|
|
||
| table := ebtables.Nat | ||
| action := ebtables.Delete | ||
|
|
||
| for rule, chain := range chainRules { | ||
| if _, ok := stateRules[rule]; !ok { | ||
| if itr, ok := networkMonitor.DeleteRulesToBeValidated[rule]; ok && itr > 0 { | ||
| log.Printf("[monitor] Deleting Ebtable rule as it didn't exist in state for %d iterations chain %v rule %v", itr, chain, rule) | ||
| if err := ebtables.SetEbRule(table, action, chain, rule); err != nil { | ||
| log.Printf("[monitor] Error while deleting ebtable rule %v", err) | ||
| } | ||
|
|
||
| delete(networkMonitor.DeleteRulesToBeValidated, rule) | ||
| } else { | ||
| log.Printf("[DELETE] Found unmatched rule chain %v rule %v itr %d. Giving one more iteration.", chain, rule, itr) | ||
| networkMonitor.DeleteRulesToBeValidated[rule] = itr + 1 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // addRulesNotExistInMap adds rules to nat Ebtable if rule was in stateRules and not in current chain rules after a certain number of iterations. | ||
| func (networkMonitor *NetworkMonitor) addRulesNotExistInMap( | ||
| stateRules map[string]string, | ||
| chainRules map[string]string) { | ||
|
|
||
| table := ebtables.Nat | ||
| action := ebtables.Append | ||
|
|
||
| for rule, chain := range stateRules { | ||
| if _, ok := chainRules[rule]; !ok { | ||
| if itr, ok := networkMonitor.AddRulesToBeValidated[rule]; ok && itr > 0 { | ||
| log.Printf("[monitor] Adding Ebtable rule as it existed in state rules but not in current chain rules for %d iterations chain %v rule %v", itr, chain, rule) | ||
| if err := ebtables.SetEbRule(table, action, chain, rule); err != nil { | ||
| log.Printf("[monitor] Error while adding ebtable rule %v", err) | ||
| } | ||
|
|
||
| delete(networkMonitor.AddRulesToBeValidated, rule) | ||
| } else { | ||
| log.Printf("[ADD] Found unmatched rule chain %v rule %v itr %d. Giving one more iteration", chain, rule, itr) | ||
| networkMonitor.AddRulesToBeValidated[rule] = itr + 1 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // CreateRequiredL2Rules finds the rules that should be in nat ebtable based on state. | ||
| func (networkMonitor *NetworkMonitor) CreateRequiredL2Rules( | ||
| currentEbtableRulesMap map[string]string, | ||
| currentStateRulesMap map[string]string) error { | ||
|
|
||
| for rule := range networkMonitor.AddRulesToBeValidated { | ||
| if _, ok := currentStateRulesMap[rule]; !ok { | ||
| delete(networkMonitor.AddRulesToBeValidated, rule) | ||
| } | ||
| } | ||
|
|
||
| networkMonitor.addRulesNotExistInMap(currentStateRulesMap, currentEbtableRulesMap) | ||
pjohnst5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
pjohnst5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| // RemoveInvalidL2Rules removes rules that should not be in nat ebtable based on state. | ||
| func (networkMonitor *NetworkMonitor) RemoveInvalidL2Rules( | ||
| currentEbtableRulesMap map[string]string, | ||
| currentStateRulesMap map[string]string) error { | ||
|
|
||
| for rule := range networkMonitor.DeleteRulesToBeValidated { | ||
| if _, ok := currentEbtableRulesMap[rule]; !ok { | ||
| delete(networkMonitor.DeleteRulesToBeValidated, rule) | ||
| } | ||
| } | ||
|
|
||
| networkMonitor.deleteRulesNotExistInMap(currentEbtableRulesMap, currentStateRulesMap) | ||
|
|
||
pjohnst5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| // generateL2RulesMap gets rules from chainName and puts them in currentEbtableRulesMap. | ||
| func generateL2RulesMap(currentEbtableRulesMap map[string]string, chainName string) error { | ||
| table := ebtables.Nat | ||
| rules, err := ebtables.GetEbtableRules(table, chainName) | ||
| if err != nil { | ||
| log.Printf("[monitor] Error while getting rules list from table %v chain %v. Error: %v", | ||
| table, chainName, err) | ||
| return err | ||
| } | ||
|
|
||
| for _, rule := range rules { | ||
| currentEbtableRulesMap[rule] = chainName | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetEbTableRulesInMap gathers prerouting and postrouting rules into a map. | ||
| func GetEbTableRulesInMap() (map[string]string, error) { | ||
| currentEbtableRulesMap := make(map[string]string) | ||
| if err := generateL2RulesMap(currentEbtableRulesMap, ebtables.PreRouting); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := generateL2RulesMap(currentEbtableRulesMap, ebtables.PostRouting); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return currentEbtableRulesMap, nil | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright 2017 Microsoft. All rights reserved. | ||
| // MIT License | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| cnms "github.com/Azure/azure-container-networking/cnms/cnmspackage" | ||
| acn "github.com/Azure/azure-container-networking/common" | ||
| "github.com/Azure/azure-container-networking/log" | ||
| "github.com/Azure/azure-container-networking/network" | ||
| "github.com/Azure/azure-container-networking/platform" | ||
| "github.com/Azure/azure-container-networking/store" | ||
| ) | ||
|
|
||
| const ( | ||
| // Service name. | ||
| name = "azure-cnimonitor" | ||
| pluginName = "azure-vnet" | ||
| DEFAULT_TIMEOUT_IN_SECS = "10" | ||
| ) | ||
|
|
||
| // Version is populated by make during build. | ||
| var version string | ||
|
|
||
| // Command line arguments for CNM plugin. | ||
| var args = acn.ArgumentList{ | ||
| { | ||
| Name: acn.OptLogLevel, | ||
| Shorthand: acn.OptLogLevelAlias, | ||
| Description: "Set the logging level", | ||
| Type: "int", | ||
| DefaultValue: acn.OptLogLevelInfo, | ||
| ValueMap: map[string]interface{}{ | ||
| acn.OptLogLevelInfo: log.LevelInfo, | ||
| acn.OptLogLevelDebug: log.LevelDebug, | ||
| }, | ||
| }, | ||
| { | ||
| Name: acn.OptLogTarget, | ||
| Shorthand: acn.OptLogTargetAlias, | ||
| Description: "Set the logging target", | ||
| Type: "int", | ||
| DefaultValue: acn.OptLogTargetFile, | ||
| ValueMap: map[string]interface{}{ | ||
| acn.OptLogTargetSyslog: log.TargetSyslog, | ||
| acn.OptLogTargetStderr: log.TargetStderr, | ||
| acn.OptLogTargetFile: log.TargetLogfile, | ||
| }, | ||
| }, | ||
| { | ||
| Name: acn.OptLogLocation, | ||
| Shorthand: acn.OptLogLocationAlias, | ||
| Description: "Set the directory location where logs will be saved", | ||
| Type: "string", | ||
| DefaultValue: "", | ||
| }, | ||
| { | ||
| Name: acn.OptIntervalTime, | ||
| Shorthand: acn.OptIntervalTimeAlias, | ||
| Description: "Periodic Interval Time", | ||
| Type: "int", | ||
| DefaultValue: DEFAULT_TIMEOUT_IN_SECS, | ||
| }, | ||
| { | ||
| Name: acn.OptVersion, | ||
| Shorthand: acn.OptVersionAlias, | ||
| Description: "Print version information", | ||
| Type: "bool", | ||
| DefaultValue: false, | ||
| }, | ||
| } | ||
|
|
||
| // Prints description and version information. | ||
| func printVersion() { | ||
| fmt.Printf("Azure Container Network Monitoring Service\n") | ||
| fmt.Printf("Version %v\n", version) | ||
| } | ||
|
|
||
| // Main is the entry point for CNMS. | ||
| func main() { | ||
| // Initialize and parse command line arguments. | ||
| acn.ParseArgs(&args, printVersion) | ||
| logLevel := acn.GetArg(acn.OptLogLevel).(int) | ||
| logTarget := acn.GetArg(acn.OptLogTarget).(int) | ||
| logDirectory := acn.GetArg(acn.OptLogLocation).(string) | ||
| timeout := acn.GetArg(acn.OptIntervalTime).(int) | ||
| vers := acn.GetArg(acn.OptVersion).(bool) | ||
| if vers { | ||
| printVersion() | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| // Initialize CNMS. | ||
| var config acn.PluginConfig | ||
| config.Version = version | ||
|
|
||
| // Create a channel to receive unhandled errors from CNMS. | ||
| config.ErrChan = make(chan error, 1) | ||
|
|
||
| var err error | ||
| // Create logging provider. | ||
| log.SetName(name) | ||
| log.SetLevel(logLevel) | ||
| if err := log.SetTargetLogDirectory(logTarget, logDirectory); err != nil { | ||
| fmt.Printf("[monitor] Failed to configure logging: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| // Log platform information. | ||
| log.Printf("[monitor] Running on %v", platform.GetOSInfo()) | ||
|
|
||
| netMonitor := &cnms.NetworkMonitor{ | ||
| AddRulesToBeValidated: make(map[string]int), | ||
| DeleteRulesToBeValidated: make(map[string]int), | ||
| } | ||
|
|
||
| for true { | ||
| config.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath + pluginName + ".json") | ||
| if err != nil { | ||
| fmt.Printf("[monitor] Failed to create store: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| nm, err := network.NewNetworkManager() | ||
| if err != nil { | ||
| log.Printf("[monitor] Failed while creating network manager") | ||
| return | ||
| } | ||
|
|
||
| if err := nm.Initialize(&config); err != nil { | ||
| log.Printf("[monitor] Failed while initializing network manager %+v", err) | ||
| } | ||
|
|
||
| log.Printf("[monitor] network manager:%+v", nm) | ||
|
|
||
| if err := nm.SetupNetworkUsingState(netMonitor); err != nil { | ||
| log.Printf("[monitor] Failed while calling SetupNetworkUsingState with error %v", err) | ||
| } | ||
|
|
||
| log.Printf("[monitor] Going to sleep for %v seconds", timeout) | ||
| time.Sleep(time.Duration(timeout) * time.Second) | ||
| nm = nil | ||
| } | ||
|
|
||
| log.Close() | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.