go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/iptablesplugin/iptablesplugin.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:generate descriptor-adapter --descriptor-name RuleChain --value-type *linux_iptables.RuleChain --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/iptables" --output-dir "descriptor" 16 17 package iptablesplugin 18 19 import ( 20 "math" 21 22 "go.ligato.io/cn-infra/v2/infra" 23 kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 24 "go.ligato.io/vpp-agent/v3/plugins/linux/iptablesplugin/descriptor" 25 "go.ligato.io/vpp-agent/v3/plugins/linux/iptablesplugin/linuxcalls" 26 "go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin" 27 ) 28 29 const ( 30 // by default, at most 10 go routines will split the configured rule chains 31 // to execute the Retrieve operation in parallel. 32 defaultGoRoutinesCnt = 10 33 34 // by default, no rules will be added by alternative performance strategy using 35 // iptables-save/modify data/iptables-store technique 36 // If this performance technique is needed, then the minimum rule limit should be lowered 37 // by configuration to some lower value (0 means that the permance strategy is 38 // always used) 39 defaultMinRuleCountForPerfRuleAddition = math.MaxInt32 40 ) 41 42 // IPTablesPlugin configures Linux iptables rules. 43 type IPTablesPlugin struct { 44 Deps 45 46 // From configuration file 47 disabled bool 48 configFound bool 49 50 // system handlers 51 iptHandler linuxcalls.IPTablesAPI 52 } 53 54 // Deps lists dependencies of the plugin. 55 type Deps struct { 56 infra.PluginDeps 57 KVScheduler kvs.KVScheduler 58 NsPlugin nsplugin.API 59 } 60 61 // Config holds the plugin configuration. 62 type Config struct { 63 linuxcalls.HandlerConfig `json:"handler"` 64 65 Disabled bool `json:"disabled"` 66 GoRoutinesCnt int `json:"go-routines-count"` 67 } 68 69 // Init initializes and registers descriptors and handlers for Linux iptables rules. 70 func (p *IPTablesPlugin) Init() error { 71 // parse configuration file 72 config, err := p.retrieveConfig() 73 if err != nil { 74 return err 75 } 76 p.Log.Debugf("Linux iptables config: %+v", config) 77 if config.Disabled { 78 p.disabled = true 79 p.Log.Infof("Disabling iptables plugin") 80 return nil 81 } 82 83 // init iptables handler 84 p.iptHandler = linuxcalls.NewIPTablesHandler() 85 err = p.iptHandler.Init(&config.HandlerConfig) 86 if err != nil && p.configFound { 87 // just warn here, iptables / ip6tables just may not be installed - will return 88 // an error by attempt to configure it 89 p.Log.Warnf("Error by initializing iptables handler: %v", err) 90 } 91 92 // init & register the descriptor 93 ruleChainDescriptor := descriptor.NewRuleChainDescriptor( 94 p.KVScheduler, p.iptHandler, p.NsPlugin, p.Log, config.GoRoutinesCnt, config.MinRuleCountForPerfRuleAddition) 95 96 err = p.Deps.KVScheduler.RegisterKVDescriptor(ruleChainDescriptor) 97 if err != nil { 98 return err 99 } 100 101 return nil 102 } 103 104 // Close does nothing here. 105 func (p *IPTablesPlugin) Close() error { 106 return nil 107 } 108 109 // retrieveConfig loads plugin configuration file. 110 func (p *IPTablesPlugin) retrieveConfig() (*Config, error) { 111 config := &Config{ 112 // default configuration 113 GoRoutinesCnt: defaultGoRoutinesCnt, 114 HandlerConfig: linuxcalls.HandlerConfig{ 115 MinRuleCountForPerfRuleAddition: defaultMinRuleCountForPerfRuleAddition, 116 }, 117 } 118 found, err := p.Cfg.LoadValue(config) 119 if !found { 120 p.Log.Debug("Linux IPTablesPlugin config not found") 121 return config, nil 122 } 123 if err != nil { 124 return nil, err 125 } 126 p.configFound = true 127 return config, err 128 }