-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from Mirantis/ivan4th/tapfdsource-test
Add TapFDSource test
- Loading branch information
Showing
10 changed files
with
1,020 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2017 Mirantis | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nettools | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
|
||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// OpenTAP opens a tap device and returns an os.File for it | ||
func OpenTAP(devName string) (*os.File, error) { | ||
tapFile, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var req ifReq | ||
|
||
// set IFF_NO_PI to not provide packet information | ||
// If flag IFF_NO_PI is not set each frame format is: | ||
// Flags [2 bytes] | ||
// Proto [2 bytes] | ||
// Raw protocol ethernet frame. | ||
// This extra 4-byte header breaks connectivity as in this case kernel truncates initial package | ||
req.Flags = uint16(syscall.IFF_TAP | syscall.IFF_NO_PI | syscall.IFF_ONE_QUEUE) | ||
copy(req.Name[:15], devName) | ||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, tapFile.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req))) | ||
if errno != 0 { | ||
return nil, fmt.Errorf("tuntap IOCTL TUNSETIFF failed, errno %v", errno) | ||
} | ||
return tapFile, nil | ||
} | ||
|
||
// CreateTAP sets up a tap link and brings it up | ||
func CreateTAP(devName string, mtu int) (netlink.Link, error) { | ||
tap := &netlink.Tuntap{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: devName, | ||
Flags: net.FlagUp, | ||
MTU: mtu, | ||
}, | ||
Mode: netlink.TUNTAP_MODE_TAP, | ||
} | ||
|
||
if err := netlink.LinkAdd(tap); err != nil { | ||
return nil, fmt.Errorf("failed to create tap interface: %v", err) | ||
} | ||
|
||
if err := netlink.LinkSetUp(tap); err != nil { | ||
return nil, fmt.Errorf("failed to set %q up: %v", devName, err) | ||
} | ||
|
||
return tap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
This directory contains network tests that make use of network | ||
namespaces and should not be mixed with non-network-namespace-aware | ||
tests. | ||
This directory contains the tests that examine Virtlet networking | ||
by running a DHCP client and sending actual network packets. | ||
|
||
For more info, see containernetworking/cni#262, vishvananda/netns#17 | ||
etc. | ||
Note that before we switch to Go 1.10, there may be test flakes | ||
because some goroutines may inherit a network namespace from other | ||
goroutines unintentionally. Running at least some of the network tests | ||
separately may help reduce the impact of this problem. For more info, | ||
see containernetworking/cni#262, vishvananda/netns#17, golang/go#20676 |
Oops, something went wrong.