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. LinkEndpoint.Attach will be called to bind ep with a NetworkDispatcher.
(id tcpip.NICID, ep LinkEndpoint, opts NICOptions)
| 935 | // |
| 936 | // LinkEndpoint.Attach will be called to bind ep with a NetworkDispatcher. |
| 937 | func (s *Stack) CreateNICWithOptions(id tcpip.NICID, ep LinkEndpoint, opts NICOptions) tcpip.Error { |
| 938 | s.mu.Lock() |
| 939 | defer s.mu.Unlock() |
| 940 | |
| 941 | if id == 0 { |
| 942 | return &tcpip.ErrInvalidNICID{} |
| 943 | } |
| 944 | // Make sure id is unique. |
| 945 | if _, ok := s.nics[id]; ok { |
| 946 | return &tcpip.ErrDuplicateNICID{} |
| 947 | } |
| 948 | |
| 949 | // Make sure name is unique, unless unnamed. |
| 950 | if opts.Name != "" { |
| 951 | for _, n := range s.nics { |
| 952 | if n.Name() == opts.Name { |
| 953 | return &tcpip.ErrDuplicateNICID{} |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | n := newNIC(s, id, ep, opts) |
| 959 | for proto := range s.defaultForwardingEnabled { |
| 960 | if _, err := n.setForwarding(proto, true); err != nil { |
| 961 | panic(fmt.Sprintf("newNIC(%d, ...).setForwarding(%d, true): %s", id, proto, err)) |
| 962 | } |
| 963 | } |
| 964 | s.nics[id] = n |
| 965 | if n.IsLoopback() { |
| 966 | s.loopbackNIC = n |
| 967 | } |
| 968 | ep.SetOnCloseAction(func() { |
| 969 | s.RemoveNIC(id) |
| 970 | }) |
| 971 | if !opts.Disabled { |
| 972 | return n.enable() |
| 973 | } |
| 974 | |
| 975 | return nil |
| 976 | } |
| 977 | |
| 978 | // CreateNIC creates a NIC with the provided id and LinkEndpoint and calls |
| 979 | // LinkEndpoint.Attach to bind ep with a NetworkDispatcher. |