python socket.connect does not seem to work on a virtual IP
I have set up some virtual IPs via:
~# ip link add link eth0 name eth0.1 address 11:22:33:44:55:66 type macvlan
~# ifconfig eth0.1 10.10.0.0/24
I am using the following code to connect from it:
sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM开发者_StackOverflow中文版)
sTCP.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, IFACE)
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT)
sTCP.connect((HOST, TCP_PORT))
print "Connected"
this works fine if IFACE is eth0, but it does not get past sTCP.connect from eth0.1 and fails in bindtodevice (as expected) on eth0.2.
Why does eth0.1 not work? Is this a python problem, or something in the linux network implementation?
I just tried this on my Fedora 13 system and it worked. I did have to make some modifications to make it work on my system, hopefully this will give you clues. Code used:
### in shell
# Used 00 for first MAC octet to avoid issues with multicast addressing
ip link add link eth0 name eth0.1 address 00:22:33:44:55:66 type macvlan
ifconfig eth0.1 10.1.23.6/25
# python
import socket
HOST = "10.1.23.30"
TCP_PORT = 80
IFACE = "eth0.1"
sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# switched to socket.SO_BINDTODEVICE since I'm not sure what "IN" referred to
# EDIT: figured out there's another module called IN, but the value is the same (25)
sTCP.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, IFACE)
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT)
sTCP.connect((HOST, TCP_PORT))
print "Connected"
I used tcpdump to prove to myself the packets were coming out of eth0.1. Perhaps you're running into a VLAN issue? Run packet captures on the client and server to see what's actually happening on the wire.
精彩评论