Sergio Morales
Sergio Morales

Reputation: 2640

Setting up VLAN on C sockets, receiving it on other end

I want to set up a C socket so that I can add 802.1Q priority tags to the UDP packets that I'll be sending through it. This is what I'm doing:

struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "wlan0.10");
ret = setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
if (ret < 0) {
    errorMsg << " Set socket options error: " << strerror(errno) << endl;
    log(errorMsg.str());
}

// this is only effective if the OS has VLAN enabled and VLAN is in use on the interface
const int VLAN_VOIP_PRIORITY = 5;
ret = setsockopt(mSocket, SOL_SOCKET, SO_PRIORITY, &VLAN_VOIP_PRIORITY, sizeof(VLAN_VOIP_PRIORITY));
if (ret < 0) {
    errorMsg << " Set socket options error: " << strerror(errno) << endl;
    log(errorMsg.str());
}

wlan0.10 is a virtual network I've set up using:

vconfig add wlan0 10
ip addr add 10.0.0.1/24 dev wlan0.10

Now, I have no idea if it's working. I tried using Wireshark both on the source and destination ends of the stream, and I can't see it (when listening on the source, I listened both on wlan0 and wlan0.10, no luck). If I remove the SO_BINDTODEVICE above though, I can see it just fine, but then I can't see any indication of the SO_PRIORITY having any effect on Wireshark either.

Upvotes: 0

Views: 6643

Answers (2)

Arvindh Srinivasan
Arvindh Srinivasan

Reputation: 1

You could set the priority via SIOCSIFVLAN.

For eg) lets assume VLAN is eth0.10
Sample code as below

struct vlan_ioctl_args vlan_args;
setsockopt(sock_fd,SOL_SOCKET,SO_PRIORITY, &priority,sizeof(priority);
vlan_args.cmd=SET_VLAN_EGRESS_PRIORITY_CMD;
vlan_args.u.skb_priority=priority;
vlan_args.qos=qos;
vlan_args.u.name_type=VLAN_NAME_TYPE_RAW_PLUS_VID;
strcpy(vlan_args.device1,"eth0.10");

ioctl (sock_fd,SIOCSIFVLAN, &vlan_args);

You can then confirm if the egress priority is set by checking cat /proc/net/vlan/eth0.10

Upvotes: 0

nos
nos

Reputation: 229058

setsockopt(mSocket, SOL_SOCKET, SO_PRIORITY would set the internal sk_priority for the packet in the kernel IP stack, not the vlan priority.

One of the things you can do with this internal priority is to map it to a vlan priority with the vconfig set_egress_map command

Upvotes: 2

Related Questions