[systemd-devel] [PATCH] networkd: Introduce tun/tap device

Susant Sahani susant at redhat.com
Mon Jun 30 09:56:20 PDT 2014


On 06/25/2014 07:12 PM, Zbigniew Jędrzejewski-Szmek wrote:

>> -        [NETDEV_KIND_VTI] = "vti"
>> +        [NETDEV_KIND_VTI] = "vti",
>> +        [NETDEV_KIND_TUN] = "tun",
>> +        [NETDEV_KIND_TAP] = "tap" <- Maybe add a comma here, to makes future patches simpler
>
>>   };
>>
>>   DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
>> @@ -221,6 +223,7 @@ static int netdev_enter_ready(NetDev *netdev) {
>>
>>           return 0;
>>   }
>> +
>>   static int netdev_create_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
>>           NetDev *netdev = userdata;
>>           int r;
>> @@ -521,11 +524,19 @@ int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *message) {
>>                   return -EINVAL;
>>           }
>>
>> -        if (!streq(kind, received_kind)) {
>> -                log_error_netdev(netdev, "Received newlink with wrong KIND %s, "
>> -                                 "expected %s", received_kind, kind);
>> -                netdev_enter_failed(netdev);
>> -                return r;
>> +        switch(netdev->kind) {
>> +        case NETDEV_KIND_TUN:
>> +        case NETDEV_KIND_TAP:
>> +                break;
>> +        default:
>> +                if (!streq(kind, received_kind)) {
>> +                        log_error_netdev(netdev,
>> +                                         "Received newlink with wrong KIND %s, "
>> +                                         "expected %s", received_kind, kind);
>> +                        netdev_enter_failed(netdev);
>> +                        return r;
>> +                }
>> +                break;
>>           }
>>
>>           netdev->ifindex = ifindex;
>> @@ -617,9 +628,10 @@ static int netdev_load_one(Manager *manager, const char *filename) {
>>           netdev->vxlanid = VXLAN_VID_MAX + 1;
>>           netdev->tunnel_pmtudisc = true;
>>           netdev->learning = true;
>> +        netdev->packet_info = true;
>>
>>           r = config_parse(NULL, filename, file,
>> -                         "Match\0NetDev\0VLAN\0MACVLAN\0VXLAN\0Tunnel\0Peer\0",
>> +                         "Match\0NetDev\0VLAN\0MACVLAN\0VXLAN\0Tunnel\0Peer\0Tun\0Tap\0",
>>                            config_item_perf_lookup, (void*) network_netdev_gperf_lookup,
>>                            false, false, netdev);
>>           if (r < 0) {
>> @@ -719,6 +731,14 @@ static int netdev_load_one(Manager *manager, const char *filename) {
>>                   if (r < 0)
>>                           return r;
>>                   break;
>> +
>> +        case NETDEV_KIND_TUN:
>> +        case NETDEV_KIND_TAP:
>> +                r = netdev_create_tuntap(netdev);
>> +                if (r < 0)
>> +                        return r;
>> +                break;
>> +
>>           default:
>>                   break;
>>           }
>> diff --git a/src/network/networkd-tuntap.c b/src/network/networkd-tuntap.c
>> new file mode 100644
>> index 0000000..7c1840c
>> --- /dev/null
>> +++ b/src/network/networkd-tuntap.c
>> @@ -0,0 +1,101 @@
>> +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
>> +
>> +/***
>> +    This file is part of systemd.
>> +
>> +    Copyright 2014 Susant Sahani <susant at redhat.com>
>> +
>> +    systemd is free software; you can redistribute it and/or modify it
>> +    under the terms of the GNU Lesser General Public License as published by
>> +    the Free Software Foundation; either version 2.1 of the License, or
>> +    (at your option) any later version.
>> +
>> +    systemd is distributed in the hope that it will be useful, but
>> +    WITHOUT ANY WARRANTY; without even the implied warranty of
>> +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
>> +    Lesser General Public License for more details.
>> +
>> +    You should have received a copy of the GNU Lesser General Public License
>> +    along with systemd; If not, see <http://www.gnu.org/licenses/>.
>> +***/
>> +
>> +#include <sys/ioctl.h>
>> +#include <net/if.h>
>> +#include <linux/if_tun.h>
>> +
>> +#include "networkd.h"
>> +
>> +#define TUN_DEV "/dev/net/tun"
>> +
>> +
>> +static int netdev_fill_tuntap_message(NetDev *netdev, struct ifreq *ifr) {
>> +
>> +        assert(netdev);
>> +        assert(ifr);
>> +
>> +        memset(ifr, 0, sizeof(*ifr));
>>
>> +        if(netdev->kind != NETDEV_KIND_TAP)
>> +                ifr->ifr_flags |= IFF_TUN;
>> +        else
>> +                ifr->ifr_flags |= IFF_TAP;
>> +
>> +        if(!netdev->packet_info)
>> +                ifr->ifr_flags &= ~IFF_NO_PI;
>> +        else
>> +                ifr->ifr_flags |= IFF_NO_PI;
> Can the conditions in two if's above be reverted? It is easier to read
> "true" conditions than "!false".
>
> Also add space between "if" and "(".

>>   /* gperf */
>>   const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
>
> Look good.
>
> Zbyszek
>

Addressed all the comments thanks .
-- 
Susant


More information about the systemd-devel mailing list