mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Implement create veth
Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
This commit is contained in:
parent
4dec36d1ee
commit
2d2c237f50
1 changed files with 53 additions and 31 deletions
|
@ -142,29 +142,61 @@ func rtaAlignOf(attrlen int) int {
|
||||||
|
|
||||||
type RtAttr struct {
|
type RtAttr struct {
|
||||||
syscall.RtAttr
|
syscall.RtAttr
|
||||||
Data []byte
|
Data []byte
|
||||||
|
children []*RtAttr
|
||||||
|
prefix int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRtAttr(attrType int, data []byte) *RtAttr {
|
func newRtAttr(attrType int, data []byte) *RtAttr {
|
||||||
attr := &RtAttr{}
|
attr := &RtAttr{
|
||||||
|
children: []*RtAttr{},
|
||||||
|
}
|
||||||
attr.Type = uint16(attrType)
|
attr.Type = uint16(attrType)
|
||||||
attr.Data = data
|
attr.Data = data
|
||||||
|
|
||||||
return attr
|
return attr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attr *RtAttr) ToWireFormat() []byte {
|
func newRtAttrChild(parent *RtAttr, attrType int, data []byte) *RtAttr {
|
||||||
|
attr := newRtAttr(attrType, data)
|
||||||
|
parent.children = append(parent.children, attr)
|
||||||
|
return attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RtAttr) length() int {
|
||||||
|
l := 0
|
||||||
|
for _, child := range a.children {
|
||||||
|
l += child.length() + syscall.SizeofRtAttr + child.prefix
|
||||||
|
}
|
||||||
|
if l == 0 {
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
return rtaAlignOf(l + len(a.Data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RtAttr) ToWireFormat() []byte {
|
||||||
native := nativeEndian()
|
native := nativeEndian()
|
||||||
|
|
||||||
len := syscall.SizeofRtAttr + len(attr.Data)
|
length := a.length()
|
||||||
b := make([]byte, rtaAlignOf(len))
|
buf := make([]byte, rtaAlignOf(length+syscall.SizeofRtAttr))
|
||||||
native.PutUint16(b[0:2], uint16(len))
|
|
||||||
native.PutUint16(b[2:4], attr.Type)
|
if a.Data != nil {
|
||||||
for i, d := range attr.Data {
|
copy(buf[4:], a.Data)
|
||||||
b[4+i] = d
|
} else {
|
||||||
|
next := 4
|
||||||
|
for _, child := range a.children {
|
||||||
|
childBuf := child.ToWireFormat()
|
||||||
|
copy(buf[next+child.prefix:], childBuf)
|
||||||
|
next += rtaAlignOf(len(childBuf))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return b
|
if l := uint16(rtaAlignOf(length)); l != 0 {
|
||||||
|
native.PutUint16(buf[0:2], l+1)
|
||||||
|
}
|
||||||
|
native.PutUint16(buf[2:4], a.Type)
|
||||||
|
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
type NetlinkRequest struct {
|
type NetlinkRequest struct {
|
||||||
|
@ -501,12 +533,7 @@ func NetworkLinkAddIp(iface *net.Interface, ip net.IP, ipNet *net.IPNet) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func zeroTerminated(s string) []byte {
|
func zeroTerminated(s string) []byte {
|
||||||
bytes := make([]byte, len(s)+1)
|
return []byte(s + "\000")
|
||||||
for i := 0; i < len(s); i++ {
|
|
||||||
bytes[i] = s[i]
|
|
||||||
}
|
|
||||||
bytes[len(s)] = 0
|
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func nonZeroTerminated(s string) []byte {
|
func nonZeroTerminated(s string) []byte {
|
||||||
|
@ -697,24 +724,19 @@ func NetworkCreateVethPair(name1, name2 string) error {
|
||||||
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
||||||
wb.AddData(msg)
|
wb.AddData(msg)
|
||||||
|
|
||||||
kindData := newRtAttr(IFLA_INFO_KIND, nonZeroTerminated("veth"))
|
|
||||||
info := newRtAttr(syscall.IFLA_LINKINFO, kindData.ToWireFormat())
|
|
||||||
// wb.AddData(info)
|
|
||||||
|
|
||||||
peerName := newRtAttr(syscall.IFLA_IFNAME, nonZeroTerminated(name2))
|
|
||||||
peer := newRtAttr(VETH_PEER, peerName.ToWireFormat())
|
|
||||||
// wb.AddData(peer)
|
|
||||||
|
|
||||||
b := []byte{}
|
|
||||||
b = append(b, peer.ToWireFormat()...)
|
|
||||||
b = append(b, info.ToWireFormat()...)
|
|
||||||
|
|
||||||
infoData := newRtAttr(IFLA_INFO_DATA, b)
|
|
||||||
wb.AddData(infoData)
|
|
||||||
|
|
||||||
nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name1))
|
nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name1))
|
||||||
wb.AddData(nameData)
|
wb.AddData(nameData)
|
||||||
|
|
||||||
|
nest1 := newRtAttr(syscall.IFLA_LINKINFO, nil)
|
||||||
|
newRtAttrChild(nest1, IFLA_INFO_KIND, zeroTerminated("veth"))
|
||||||
|
nest2 := newRtAttrChild(nest1, IFLA_INFO_DATA, nil)
|
||||||
|
nest3 := newRtAttrChild(nest2, VETH_PEER, nil)
|
||||||
|
|
||||||
|
last := newRtAttrChild(nest3, syscall.IFLA_IFNAME, zeroTerminated(name2))
|
||||||
|
last.prefix = syscall.SizeofIfInfomsg
|
||||||
|
|
||||||
|
wb.AddData(nest1)
|
||||||
|
|
||||||
if err := s.Send(wb); err != nil {
|
if err := s.Send(wb); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue