Combine various Create*NIC methods into CreateNICWithOptions.

PiperOrigin-RevId: 288779416
This commit is contained in:
Bert Muthalaly
2020-01-08 14:50:49 -08:00
committed by gVisor bot
parent a271bccfc6
commit e21c584056
8 changed files with 47 additions and 45 deletions
+3 -3
View File
@@ -2500,9 +2500,9 @@ func TestAutoGenAddrWithOpaqueIID(t *testing.T) {
SecretKey: secretKey,
},
})
if err := s.CreateNamedNIC(nicID, nicName, e); err != nil {
t.Fatalf("CreateNamedNIC(%d, %q, _) = %s", nicID, nicName, err)
opts := stack.NICOptions{Name: nicName}
if err := s.CreateNICWithOptions(nicID, e, opts); err != nil {
t.Fatalf("CreateNICWithOptions(%d, _, %+v, _) = %s", nicID, opts, err)
}
expectAutoGenAddrEvent := func(addr tcpip.AddressWithPrefix, eventType ndpAutoGenAddrEventType) {
+20 -26
View File
@@ -796,9 +796,21 @@ func (s *Stack) NewPacketEndpoint(cooked bool, netProto tcpip.NetworkProtocolNum
return s.rawFactory.NewPacketEndpoint(s, cooked, netProto, waiterQueue)
}
// createNIC creates a NIC with the provided id and link-layer endpoint, and
// optionally enable it.
func (s *Stack) createNIC(id tcpip.NICID, name string, ep LinkEndpoint, enabled bool) *tcpip.Error {
// NICOptions specifies the configuration of a NIC as it is being created.
// The zero value creates an enabled, unnamed NIC.
type NICOptions struct {
// Name specifies the name of the NIC.
Name string
// Disabled specifies whether to avoid calling Attach on the passed
// LinkEndpoint.
Disabled bool
}
// CreateNICWithOptions creates a NIC with the provided id, LinkEndpoint, and
// NICOptions. See the documentation on type NICOptions for details on how
// NICs can be configured.
func (s *Stack) CreateNICWithOptions(id tcpip.NICID, ep LinkEndpoint, opts NICOptions) *tcpip.Error {
s.mu.Lock()
defer s.mu.Unlock()
@@ -807,38 +819,20 @@ func (s *Stack) createNIC(id tcpip.NICID, name string, ep LinkEndpoint, enabled
return tcpip.ErrDuplicateNICID
}
n := newNIC(s, id, name, ep)
n := newNIC(s, id, opts.Name, ep)
s.nics[id] = n
if enabled {
if !opts.Disabled {
return n.enable()
}
return nil
}
// CreateNIC creates a NIC with the provided id and link-layer endpoint.
// CreateNIC creates a NIC with the provided id and LinkEndpoint and calls
// `LinkEndpoint.Attach` to start delivering packets to it.
func (s *Stack) CreateNIC(id tcpip.NICID, ep LinkEndpoint) *tcpip.Error {
return s.createNIC(id, "", ep, true)
}
// CreateNamedNIC creates a NIC with the provided id and link-layer endpoint,
// and a human-readable name.
func (s *Stack) CreateNamedNIC(id tcpip.NICID, name string, ep LinkEndpoint) *tcpip.Error {
return s.createNIC(id, name, ep, true)
}
// CreateDisabledNIC creates a NIC with the provided id and link-layer endpoint,
// but leave it disable. Stack.EnableNIC must be called before the link-layer
// endpoint starts delivering packets to it.
func (s *Stack) CreateDisabledNIC(id tcpip.NICID, ep LinkEndpoint) *tcpip.Error {
return s.createNIC(id, "", ep, false)
}
// CreateDisabledNamedNIC is a combination of CreateNamedNIC and
// CreateDisabledNIC.
func (s *Stack) CreateDisabledNamedNIC(id tcpip.NICID, name string, ep LinkEndpoint) *tcpip.Error {
return s.createNIC(id, name, ep, false)
return s.CreateNICWithOptions(id, ep, NICOptions{})
}
// EnableNIC enables the given NIC so that the link-layer endpoint can start
+6 -4
View File
@@ -2097,8 +2097,9 @@ func TestNICAutoGenAddrWithOpaque(t *testing.T) {
e := channel.New(10, 1280, test.linkAddr)
s := stack.New(opts)
if err := s.CreateNamedNIC(nicID, test.nicName, e); err != nil {
t.Fatalf("CreateNamedNIC(%d, %q, _) = %s", nicID, test.nicName, err)
nicOpts := stack.NICOptions{Name: test.nicName}
if err := s.CreateNICWithOptions(nicID, e, nicOpts); err != nil {
t.Fatalf("CreateNICWithOptions(%d, _, %+v) = %s", nicID, opts, err)
}
addr, err := s.GetMainNICAddress(nicID, header.IPv6ProtocolNumber)
@@ -2156,8 +2157,9 @@ func TestNoLinkLocalAutoGenForLoopbackNIC(t *testing.T) {
e := loopback.New()
s := stack.New(opts)
if err := s.CreateNamedNIC(nicID, nicName, e); err != nil {
t.Fatalf("CreateNamedNIC(%d, %q, _) = %s", nicID, nicName, err)
nicOpts := stack.NICOptions{Name: nicName}
if err := s.CreateNICWithOptions(nicID, e, nicOpts); err != nil {
t.Fatalf("CreateNICWithOptions(%d, _, %+v) = %s", nicID, nicOpts, err)
}
addr, err := s.GetMainNICAddress(nicID, header.IPv6ProtocolNumber)
+3 -2
View File
@@ -80,8 +80,9 @@ func newDualTestContextMultiNic(t *testing.T, mtu uint32, linkEpNames []string)
for i, linkEpName := range linkEpNames {
channelEP := channel.New(256, mtu, "")
nicID := tcpip.NICID(i + 1)
if err := s.CreateNamedNIC(nicID, linkEpName, channelEP); err != nil {
t.Fatalf("CreateNIC failed: %v", err)
opts := stack.NICOptions{Name: linkEpName}
if err := s.CreateNICWithOptions(nicID, channelEP, opts); err != nil {
t.Fatalf("CreateNICWithOptions(_, _, %+v) failed: %v", opts, err)
}
linkEPs[linkEpName] = channelEP
+3 -2
View File
@@ -3794,8 +3794,9 @@ func TestBindToDeviceOption(t *testing.T) {
}
defer ep.Close()
if err := s.CreateNamedNIC(321, "my_device", loopback.New()); err != nil {
t.Errorf("CreateNamedNIC failed: %v", err)
opts := stack.NICOptions{Name: "my_device"}
if err := s.CreateNICWithOptions(321, loopback.New(), opts); err != nil {
t.Errorf("CreateNICWithOptions(_, _, %+v) failed: %v", opts, err)
}
// Make an nameless NIC.
@@ -158,15 +158,17 @@ func New(t *testing.T, mtu uint32) *Context {
if testing.Verbose() {
wep = sniffer.New(ep)
}
if err := s.CreateNamedNIC(1, "nic1", wep); err != nil {
t.Fatalf("CreateNIC failed: %v", err)
opts := stack.NICOptions{Name: "nic1"}
if err := s.CreateNICWithOptions(1, wep, opts); err != nil {
t.Fatalf("CreateNICWithOptions(_, _, %+v) failed: %v", opts, err)
}
wep2 := stack.LinkEndpoint(channel.New(1000, mtu, ""))
if testing.Verbose() {
wep2 = sniffer.New(channel.New(1000, mtu, ""))
}
if err := s.CreateNamedNIC(2, "nic2", wep2); err != nil {
t.Fatalf("CreateNIC failed: %v", err)
opts2 := stack.NICOptions{Name: "nic2"}
if err := s.CreateNICWithOptions(2, wep2, opts2); err != nil {
t.Fatalf("CreateNICWithOptions(_, _, %+v) failed: %v", opts2, err)
}
if err := s.AddAddress(1, ipv4.ProtocolNumber, StackAddr); err != nil {
+3 -2
View File
@@ -508,8 +508,9 @@ func TestBindToDeviceOption(t *testing.T) {
}
defer ep.Close()
if err := s.CreateNamedNIC(321, "my_device", loopback.New()); err != nil {
t.Errorf("CreateNamedNIC failed: %v", err)
opts := stack.NICOptions{Name: "my_device"}
if err := s.CreateNICWithOptions(321, loopback.New(), opts); err != nil {
t.Errorf("CreateNICWithOptions(_, _, %+v) failed: %v", opts, err)
}
// Make an nameless NIC.
+3 -2
View File
@@ -219,8 +219,9 @@ func (n *Network) CreateLinksAndRoutes(args *CreateLinksAndRoutesArgs, _ *struct
// createNICWithAddrs creates a NIC in the network stack and adds the given
// addresses.
func (n *Network) createNICWithAddrs(id tcpip.NICID, name string, ep stack.LinkEndpoint, addrs []net.IP) error {
if err := n.Stack.CreateNamedNIC(id, name, sniffer.New(ep)); err != nil {
return fmt.Errorf("CreateNamedNIC(%v, %v, %v) failed: %v", id, name, ep, err)
opts := stack.NICOptions{Name: name}
if err := n.Stack.CreateNICWithOptions(id, sniffer.New(ep), opts); err != nil {
return fmt.Errorf("CreateNICWithOptions(%d, _, %+v) failed: %v", id, opts, err)
}
// Always start with an arp address for the NIC.