-
Notifications
You must be signed in to change notification settings - Fork 790
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
Implement CNI v1.1 support #1021
base: main
Are you sure you want to change the base?
Changes from all commits
2f5fb5b
156e37e
68a181f
c5eabbd
c66b165
0017d39
b7ae92e
574fa51
54dd2e4
76e191e
6cce47d
1bc6fde
42757d5
d8bf923
dbd92f3
650d9ad
8dcc499
34dca5b
a0374ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,46 @@ func TeardownIPMasq(ipn *net.IPNet, chain string, comment string) error { | |
return nil | ||
} | ||
|
||
func CheckIPMasq(ipn *net.IPNet, chain, comment string) error { | ||
isV6 := ipn.IP.To4() == nil | ||
|
||
var ipt *iptables.IPTables | ||
var err error | ||
var multicastNet string | ||
var ip string // the ip and its full-length prefix | ||
|
||
if isV6 { | ||
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.
|
||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6) | ||
multicastNet = "ff00::/8" | ||
ip = ipn.IP.String() + "/128" | ||
} else { | ||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4) | ||
multicastNet = "224.0.0.0/4" | ||
ip = ipn.IP.String() + "/32" | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to locate iptables: %v", err) | ||
} | ||
|
||
ok, err := ipt.Exists("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment) | ||
if err != nil { | ||
return fmt.Errorf("could not check for expected rule: %w", err) | ||
} | ||
if !ok { | ||
return fmt.Errorf("expected rule did not exist in chain %s", chain) | ||
} | ||
|
||
ok, err = ipt.Exists("nat", "POSTROUTING", "-s", ip, "-j", chain, "-m", "comment", "--comment", comment) | ||
if err != nil { | ||
return fmt.Errorf("could not check for expected rule [-A POSTROUTING ]: %w", err) | ||
} | ||
if !ok { | ||
want := []string{"-A", "POSTROUTING", "-s", ip, "-j", chain, "-m", "comment", "--comment", comment} | ||
return fmt.Errorf("expected rule %v did not exist in chain POSTROUTING", want) | ||
} | ||
return nil | ||
} | ||
|
||
// isNotExist returnst true if the error is from iptables indicating | ||
// that the target does not exist. | ||
func isNotExist(err error) bool { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package leasepool | ||
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. As far as I understand, this code comes from https://github.com/d2g/dhcp4server/, licensed as Mozilla Public License 2.0(MPL2.0) and it includes this code, not vendoring. Can we copy this file without LICENSE notification? (I mean that dhcp4server is MPL2.0, not APL2.0). Should we add MPL2.0 in LICENSE file? |
||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type LeaseStatus int | ||
|
||
const ( | ||
Free LeaseStatus = 0 | ||
Reserved LeaseStatus = 1 | ||
Active LeaseStatus = 2 | ||
) | ||
|
||
type Lease struct { | ||
IP net.IP // The IP of the Lease | ||
Status LeaseStatus // Are Reserved, Active or Free | ||
MACAddress net.HardwareAddr // Mac Address of the Device | ||
ClientID []byte // ClientID of the request | ||
Hostname string // Hostname From option 12 | ||
Expiry time.Time // Expiry Time | ||
} | ||
|
||
// leaseMarshal is a mirror of Lease used for marshalling, since | ||
// net.HardwareAddr has no native marshalling capability. | ||
type leaseMarshal struct { | ||
IP string | ||
Status int | ||
MACAddress string | ||
ClientID string | ||
Hostname string | ||
Expiry time.Time | ||
} | ||
|
||
func (l Lease) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(leaseMarshal{ | ||
IP: l.IP.String(), | ||
Status: int(l.Status), | ||
MACAddress: l.MACAddress.String(), | ||
ClientID: hex.EncodeToString(l.ClientID), | ||
Hostname: l.Hostname, | ||
Expiry: l.Expiry, | ||
}) | ||
} | ||
|
||
func (l *Lease) UnmarshalJSON(data []byte) error { | ||
stringUnMarshal := leaseMarshal{} | ||
err := json.Unmarshal(data, &stringUnMarshal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
l.IP = net.ParseIP(stringUnMarshal.IP) | ||
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. Maybe check if the parse failed here. 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. This whole file is just a copy-paste of an old dependency. It's silly code just for testing; I didn't want to fix it up. |
||
l.Status = LeaseStatus(stringUnMarshal.Status) | ||
if stringUnMarshal.MACAddress != "" { | ||
l.MACAddress, err = net.ParseMAC(stringUnMarshal.MACAddress) | ||
if err != nil { | ||
return fmt.Errorf("error parsing MAC address: %v", err) | ||
} | ||
} | ||
l.ClientID, err = hex.DecodeString(stringUnMarshal.ClientID) | ||
if err != nil { | ||
return fmt.Errorf("error decoding clientID: %v", err) | ||
} | ||
l.Hostname = stringUnMarshal.Hostname | ||
l.Expiry = stringUnMarshal.Expiry | ||
|
||
return nil | ||
} | ||
|
||
func (l Lease) Equal(other Lease) bool { | ||
if !l.IP.Equal(other.IP) { | ||
return false | ||
} | ||
|
||
if int(l.Status) != int(other.Status) { | ||
return false | ||
} | ||
|
||
if l.MACAddress.String() != other.MACAddress.String() { | ||
return false | ||
} | ||
|
||
if !bytes.Equal(l.ClientID, other.ClientID) { | ||
return false | ||
} | ||
|
||
if l.Hostname != other.Hostname { | ||
return false | ||
} | ||
|
||
if !l.Expiry.Equal(other.Expiry) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,17 +3,18 @@ package memorypool | |||||
import ( | ||||||
"bytes" | ||||||
"errors" | ||||||
"github.com/d2g/dhcp4server/leasepool" | ||||||
"net" | ||||||
"sync" | ||||||
|
||||||
"github.com/containernetworking/plugins/pkg/testutils/dhcp4server/leasepool" | ||||||
) | ||||||
|
||||||
type MemoryPool struct { | ||||||
pool []leasepool.Lease | ||||||
poolLock sync.Mutex | ||||||
} | ||||||
|
||||||
//Add A Lease To The Pool | ||||||
// Add A Lease To The Pool | ||||||
func (t *MemoryPool) AddLease(newLease leasepool.Lease) error { | ||||||
t.poolLock.Lock() | ||||||
defer t.poolLock.Unlock() | ||||||
|
@@ -24,7 +25,7 @@ func (t *MemoryPool) AddLease(newLease leasepool.Lease) error { | |||||
|
||||||
for i := range t.pool { | ||||||
if t.pool[i].IP.Equal(newLease.IP) { | ||||||
//Lease Already Exists In Pool | ||||||
// Lease Already Exists In Pool | ||||||
return errors.New("Error: Lease IP \"" + newLease.IP.String() + "\" alreay exists in Pool") | ||||||
} | ||||||
} | ||||||
|
@@ -33,18 +34,18 @@ func (t *MemoryPool) AddLease(newLease leasepool.Lease) error { | |||||
return nil | ||||||
} | ||||||
|
||||||
//Remove a Lease From The Pool | ||||||
// Remove a Lease From The Pool | ||||||
func (t *MemoryPool) RemoveLease(leaseIP net.IP) error { | ||||||
t.poolLock.Lock() | ||||||
defer t.poolLock.Unlock() | ||||||
|
||||||
for i := range t.pool { | ||||||
if t.pool[i].IP.Equal(leaseIP) { | ||||||
|
||||||
//Move the Last Element to This Position. | ||||||
// Move the Last Element to This Position. | ||||||
t.pool[i] = t.pool[len(t.pool)-1] | ||||||
|
||||||
//Shortern the Pool By One. | ||||||
// Shortern the Pool By One. | ||||||
t.pool = t.pool[0:(len(t.pool) - 1)] | ||||||
return nil | ||||||
} | ||||||
|
@@ -53,7 +54,7 @@ func (t *MemoryPool) RemoveLease(leaseIP net.IP) error { | |||||
return errors.New("Error: Lease IP \"" + leaseIP.String() + "\" Is Not In The Pool") | ||||||
} | ||||||
|
||||||
//Remove All Leases from the Pool (Required for Persistant LeaseManagers) | ||||||
// Remove All Leases from the Pool (Required for Persistent LeaseManagers) | ||||||
func (t *MemoryPool) PurgeLeases() error { | ||||||
t.poolLock.Lock() | ||||||
defer t.poolLock.Unlock() | ||||||
|
@@ -89,7 +90,7 @@ func makeKey(macAddress net.HardwareAddr, clientID []byte) []byte { | |||||
return key | ||||||
} | ||||||
|
||||||
//Get the lease already in use by that hardware address and/or client identifier. | ||||||
// Get the lease already in use by that hardware address and/or client identifier. | ||||||
func (t *MemoryPool) GetLeaseForClient(macAddress net.HardwareAddr, clientID []byte) (bool, leasepool.Lease, error) { | ||||||
t.poolLock.Lock() | ||||||
defer t.poolLock.Unlock() | ||||||
|
@@ -113,15 +114,15 @@ func (t *MemoryPool) GetNextFreeLease() (bool, leasepool.Lease, error) { | |||||
t.poolLock.Lock() | ||||||
defer t.poolLock.Unlock() | ||||||
|
||||||
//Loop Through the elements backwards. | ||||||
// Loop Through the elements backwards. | ||||||
for i := (len(t.pool) - 1); i >= 0; i-- { | ||||||
//If the Lease Is Free | ||||||
// If the Lease Is Free | ||||||
if t.pool[i].Status == leasepool.Free { | ||||||
//Take the Element | ||||||
// Take the Element | ||||||
iLease := t.pool[i] | ||||||
//Shrink the Pool By 1 | ||||||
// Shrink the Pool By 1 | ||||||
t.pool = t.pool[:(len(t.pool) - 1)] | ||||||
//Place the Lease At the Begining (This saves us having some sort of counter...) | ||||||
// Place the Lease At the Beginning (This saves us having some sort of counter...) | ||||||
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.
Suggested change
ultra-nit |
||||||
t.pool = append([]leasepool.Lease{iLease}, t.pool...) | ||||||
return true, iLease, nil | ||||||
} | ||||||
|
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.
Missing comments for the function?