linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_ipip.c
<<
>>
Prefs
   1/*
   2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_ipip.c
   3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
   4 * Copyright (c) 2017-2018 Petr Machata <petrm@mellanox.com>
   5 *
   6 * Redistribution and use in source and binary forms, with or without
   7 * modification, are permitted provided that the following conditions are met:
   8 *
   9 * 1. Redistributions of source code must retain the above copyright
  10 *    notice, this list of conditions and the following disclaimer.
  11 * 2. Redistributions in binary form must reproduce the above copyright
  12 *    notice, this list of conditions and the following disclaimer in the
  13 *    documentation and/or other materials provided with the distribution.
  14 * 3. Neither the names of the copyright holders nor the names of its
  15 *    contributors may be used to endorse or promote products derived from
  16 *    this software without specific prior written permission.
  17 *
  18 * Alternatively, this software may be distributed under the terms of the
  19 * GNU General Public License ("GPL") version 2 as published by the Free
  20 * Software Foundation.
  21 *
  22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32 * POSSIBILITY OF SUCH DAMAGE.
  33 */
  34
  35#include <linux/if.h>
  36#include <net/ip_tunnels.h>
  37#include <net/ip6_tunnel.h>
  38
  39#include "spectrum_ipip.h"
  40
  41struct ip_tunnel_parm
  42mlxsw_sp_ipip_netdev_parms4(const struct net_device *ol_dev)
  43{
  44        struct ip_tunnel *tun = netdev_priv(ol_dev);
  45
  46        return tun->parms;
  47}
  48
  49struct __ip6_tnl_parm
  50mlxsw_sp_ipip_netdev_parms6(const struct net_device *ol_dev)
  51{
  52        struct ip6_tnl *tun = netdev_priv(ol_dev);
  53
  54        return tun->parms;
  55}
  56
  57static bool mlxsw_sp_ipip_parms4_has_ikey(struct ip_tunnel_parm parms)
  58{
  59        return !!(parms.i_flags & TUNNEL_KEY);
  60}
  61
  62static bool mlxsw_sp_ipip_parms4_has_okey(struct ip_tunnel_parm parms)
  63{
  64        return !!(parms.o_flags & TUNNEL_KEY);
  65}
  66
  67static u32 mlxsw_sp_ipip_parms4_ikey(struct ip_tunnel_parm parms)
  68{
  69        return mlxsw_sp_ipip_parms4_has_ikey(parms) ?
  70                be32_to_cpu(parms.i_key) : 0;
  71}
  72
  73static u32 mlxsw_sp_ipip_parms4_okey(struct ip_tunnel_parm parms)
  74{
  75        return mlxsw_sp_ipip_parms4_has_okey(parms) ?
  76                be32_to_cpu(parms.o_key) : 0;
  77}
  78
  79static union mlxsw_sp_l3addr
  80mlxsw_sp_ipip_parms4_saddr(struct ip_tunnel_parm parms)
  81{
  82        return (union mlxsw_sp_l3addr) { .addr4 = parms.iph.saddr };
  83}
  84
  85static union mlxsw_sp_l3addr
  86mlxsw_sp_ipip_parms6_saddr(struct __ip6_tnl_parm parms)
  87{
  88        return (union mlxsw_sp_l3addr) { .addr6 = parms.laddr };
  89}
  90
  91static union mlxsw_sp_l3addr
  92mlxsw_sp_ipip_parms4_daddr(struct ip_tunnel_parm parms)
  93{
  94        return (union mlxsw_sp_l3addr) { .addr4 = parms.iph.daddr };
  95}
  96
  97static union mlxsw_sp_l3addr
  98mlxsw_sp_ipip_parms6_daddr(struct __ip6_tnl_parm parms)
  99{
 100        return (union mlxsw_sp_l3addr) { .addr6 = parms.raddr };
 101}
 102
 103union mlxsw_sp_l3addr
 104mlxsw_sp_ipip_netdev_saddr(enum mlxsw_sp_l3proto proto,
 105                           const struct net_device *ol_dev)
 106{
 107        struct ip_tunnel_parm parms4;
 108        struct __ip6_tnl_parm parms6;
 109
 110        switch (proto) {
 111        case MLXSW_SP_L3_PROTO_IPV4:
 112                parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
 113                return mlxsw_sp_ipip_parms4_saddr(parms4);
 114        case MLXSW_SP_L3_PROTO_IPV6:
 115                parms6 = mlxsw_sp_ipip_netdev_parms6(ol_dev);
 116                return mlxsw_sp_ipip_parms6_saddr(parms6);
 117        }
 118
 119        WARN_ON(1);
 120        return (union mlxsw_sp_l3addr) {0};
 121}
 122
 123static __be32 mlxsw_sp_ipip_netdev_daddr4(const struct net_device *ol_dev)
 124{
 125
 126        struct ip_tunnel_parm parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
 127
 128        return mlxsw_sp_ipip_parms4_daddr(parms4).addr4;
 129}
 130
 131static union mlxsw_sp_l3addr
 132mlxsw_sp_ipip_netdev_daddr(enum mlxsw_sp_l3proto proto,
 133                           const struct net_device *ol_dev)
 134{
 135        struct ip_tunnel_parm parms4;
 136        struct __ip6_tnl_parm parms6;
 137
 138        switch (proto) {
 139        case MLXSW_SP_L3_PROTO_IPV4:
 140                parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
 141                return mlxsw_sp_ipip_parms4_daddr(parms4);
 142        case MLXSW_SP_L3_PROTO_IPV6:
 143                parms6 = mlxsw_sp_ipip_netdev_parms6(ol_dev);
 144                return mlxsw_sp_ipip_parms6_daddr(parms6);
 145        }
 146
 147        WARN_ON(1);
 148        return (union mlxsw_sp_l3addr) {0};
 149}
 150
 151bool mlxsw_sp_l3addr_is_zero(union mlxsw_sp_l3addr addr)
 152{
 153        union mlxsw_sp_l3addr naddr = {0};
 154
 155        return !memcmp(&addr, &naddr, sizeof(naddr));
 156}
 157
 158static int
 159mlxsw_sp_ipip_nexthop_update_gre4(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
 160                                  struct mlxsw_sp_ipip_entry *ipip_entry)
 161{
 162        u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
 163        __be32 daddr4 = mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev);
 164        char ratr_pl[MLXSW_REG_RATR_LEN];
 165
 166        mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
 167                            true, MLXSW_REG_RATR_TYPE_IPIP,
 168                            adj_index, rif_index);
 169        mlxsw_reg_ratr_ipip4_entry_pack(ratr_pl, be32_to_cpu(daddr4));
 170
 171        return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
 172}
 173
 174static int
 175mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(struct mlxsw_sp *mlxsw_sp,
 176                                     u32 tunnel_index,
 177                                     struct mlxsw_sp_ipip_entry *ipip_entry)
 178{
 179        u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
 180        char rtdp_pl[MLXSW_REG_RTDP_LEN];
 181        struct ip_tunnel_parm parms;
 182        unsigned int type_check;
 183        bool has_ikey;
 184        u32 daddr4;
 185        u32 ikey;
 186
 187        parms = mlxsw_sp_ipip_netdev_parms4(ipip_entry->ol_dev);
 188        has_ikey = mlxsw_sp_ipip_parms4_has_ikey(parms);
 189        ikey = mlxsw_sp_ipip_parms4_ikey(parms);
 190
 191        mlxsw_reg_rtdp_pack(rtdp_pl, MLXSW_REG_RTDP_TYPE_IPIP, tunnel_index);
 192
 193        type_check = has_ikey ?
 194                MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE_KEY :
 195                MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE;
 196
 197        /* Linux demuxes tunnels based on packet SIP (which must match tunnel
 198         * remote IP). Thus configure decap so that it filters out packets that
 199         * are not IPv4 or have the wrong SIP. IPIP_DECAP_ERROR trap is
 200         * generated for packets that fail this criterion. Linux then handles
 201         * such packets in slow path and generates ICMP destination unreachable.
 202         */
 203        daddr4 = be32_to_cpu(mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev));
 204        mlxsw_reg_rtdp_ipip4_pack(rtdp_pl, rif_index,
 205                                  MLXSW_REG_RTDP_IPIP_SIP_CHECK_FILTER_IPV4,
 206                                  type_check, has_ikey, daddr4, ikey);
 207
 208        return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rtdp), rtdp_pl);
 209}
 210
 211static int
 212mlxsw_sp_ipip_fib_entry_op_gre4_ralue(struct mlxsw_sp *mlxsw_sp,
 213                                      u32 dip, u8 prefix_len, u16 ul_vr_id,
 214                                      enum mlxsw_reg_ralue_op op,
 215                                      u32 tunnel_index)
 216{
 217        char ralue_pl[MLXSW_REG_RALUE_LEN];
 218
 219        mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_REG_RALXX_PROTOCOL_IPV4, op,
 220                              ul_vr_id, prefix_len, dip);
 221        mlxsw_reg_ralue_act_ip2me_tun_pack(ralue_pl, tunnel_index);
 222        return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
 223}
 224
 225static int mlxsw_sp_ipip_fib_entry_op_gre4(struct mlxsw_sp *mlxsw_sp,
 226                                        struct mlxsw_sp_ipip_entry *ipip_entry,
 227                                        enum mlxsw_reg_ralue_op op,
 228                                        u32 tunnel_index)
 229{
 230        u16 ul_vr_id = mlxsw_sp_ipip_lb_ul_vr_id(ipip_entry->ol_lb);
 231        __be32 dip;
 232        int err;
 233
 234        err = mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(mlxsw_sp, tunnel_index,
 235                                                   ipip_entry);
 236        if (err)
 237                return err;
 238
 239        dip = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
 240                                         ipip_entry->ol_dev).addr4;
 241        return mlxsw_sp_ipip_fib_entry_op_gre4_ralue(mlxsw_sp, be32_to_cpu(dip),
 242                                                     32, ul_vr_id, op,
 243                                                     tunnel_index);
 244}
 245
 246static bool mlxsw_sp_ipip_tunnel_complete(enum mlxsw_sp_l3proto proto,
 247                                          const struct net_device *ol_dev)
 248{
 249        union mlxsw_sp_l3addr saddr = mlxsw_sp_ipip_netdev_saddr(proto, ol_dev);
 250        union mlxsw_sp_l3addr daddr = mlxsw_sp_ipip_netdev_daddr(proto, ol_dev);
 251
 252        /* Tunnels with unset local or remote address are valid in Linux and
 253         * used for lightweight tunnels (LWT) and Non-Broadcast Multi-Access
 254         * (NBMA) tunnels. In principle these can be offloaded, but the driver
 255         * currently doesn't support this. So punt.
 256         */
 257        return !mlxsw_sp_l3addr_is_zero(saddr) &&
 258               !mlxsw_sp_l3addr_is_zero(daddr);
 259}
 260
 261static bool mlxsw_sp_ipip_can_offload_gre4(const struct mlxsw_sp *mlxsw_sp,
 262                                           const struct net_device *ol_dev,
 263                                           enum mlxsw_sp_l3proto ol_proto)
 264{
 265        struct ip_tunnel *tunnel = netdev_priv(ol_dev);
 266        __be16 okflags = TUNNEL_KEY; /* We can't offload any other features. */
 267        bool inherit_ttl = tunnel->parms.iph.ttl == 0;
 268        bool inherit_tos = tunnel->parms.iph.tos & 0x1;
 269
 270        return (tunnel->parms.i_flags & ~okflags) == 0 &&
 271               (tunnel->parms.o_flags & ~okflags) == 0 &&
 272               inherit_ttl && inherit_tos &&
 273               mlxsw_sp_ipip_tunnel_complete(MLXSW_SP_L3_PROTO_IPV4, ol_dev);
 274}
 275
 276static struct mlxsw_sp_rif_ipip_lb_config
 277mlxsw_sp_ipip_ol_loopback_config_gre4(struct mlxsw_sp *mlxsw_sp,
 278                                      const struct net_device *ol_dev)
 279{
 280        struct ip_tunnel_parm parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
 281        enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
 282
 283        lb_ipipt = mlxsw_sp_ipip_parms4_has_okey(parms) ?
 284                MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_KEY_IN_IP :
 285                MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_IN_IP;
 286        return (struct mlxsw_sp_rif_ipip_lb_config){
 287                .lb_ipipt = lb_ipipt,
 288                .okey = mlxsw_sp_ipip_parms4_okey(parms),
 289                .ul_protocol = MLXSW_SP_L3_PROTO_IPV4,
 290                .saddr = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
 291                                                    ol_dev),
 292        };
 293}
 294
 295static int
 296mlxsw_sp_ipip_ol_netdev_change_gre4(struct mlxsw_sp *mlxsw_sp,
 297                                    struct mlxsw_sp_ipip_entry *ipip_entry)
 298{
 299        union mlxsw_sp_l3addr old_saddr, new_saddr;
 300        union mlxsw_sp_l3addr old_daddr, new_daddr;
 301        struct ip_tunnel_parm new_parms;
 302        bool update_tunnel = false;
 303        bool update_decap = false;
 304        bool update_nhs = false;
 305        int err = 0;
 306
 307        new_parms = mlxsw_sp_ipip_netdev_parms4(ipip_entry->ol_dev);
 308
 309        new_saddr = mlxsw_sp_ipip_parms4_saddr(new_parms);
 310        old_saddr = mlxsw_sp_ipip_parms4_saddr(ipip_entry->parms4);
 311        new_daddr = mlxsw_sp_ipip_parms4_daddr(new_parms);
 312        old_daddr = mlxsw_sp_ipip_parms4_daddr(ipip_entry->parms4);
 313
 314        if (!mlxsw_sp_l3addr_eq(&new_saddr, &old_saddr)) {
 315                u16 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
 316
 317                /* Since the local address has changed, if there is another
 318                 * tunnel with a matching saddr, both need to be demoted.
 319                 */
 320                if (mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp,
 321                                                         MLXSW_SP_L3_PROTO_IPV4,
 322                                                         new_saddr, ul_tb_id,
 323                                                         ipip_entry)) {
 324                        mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
 325                        return 0;
 326                }
 327
 328                update_tunnel = true;
 329        } else if ((mlxsw_sp_ipip_parms4_okey(ipip_entry->parms4) !=
 330                    mlxsw_sp_ipip_parms4_okey(new_parms)) ||
 331                   ipip_entry->parms4.link != new_parms.link) {
 332                update_tunnel = true;
 333        } else if (!mlxsw_sp_l3addr_eq(&new_daddr, &old_daddr)) {
 334                update_nhs = true;
 335        } else if (mlxsw_sp_ipip_parms4_ikey(ipip_entry->parms4) !=
 336                   mlxsw_sp_ipip_parms4_ikey(new_parms)) {
 337                update_decap = true;
 338        }
 339
 340        if (update_tunnel)
 341                err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
 342                                                          true, true, true);
 343        else if (update_nhs)
 344                err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
 345                                                          false, false, true);
 346        else if (update_decap)
 347                err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
 348                                                          false, false, false);
 349
 350        ipip_entry->parms4 = new_parms;
 351        return err;
 352}
 353
 354static const struct mlxsw_sp_ipip_ops mlxsw_sp_ipip_gre4_ops = {
 355        .dev_type = ARPHRD_IPGRE,
 356        .ul_proto = MLXSW_SP_L3_PROTO_IPV4,
 357        .nexthop_update = mlxsw_sp_ipip_nexthop_update_gre4,
 358        .fib_entry_op = mlxsw_sp_ipip_fib_entry_op_gre4,
 359        .can_offload = mlxsw_sp_ipip_can_offload_gre4,
 360        .ol_loopback_config = mlxsw_sp_ipip_ol_loopback_config_gre4,
 361        .ol_netdev_change = mlxsw_sp_ipip_ol_netdev_change_gre4,
 362};
 363
 364const struct mlxsw_sp_ipip_ops *mlxsw_sp_ipip_ops_arr[] = {
 365        [MLXSW_SP_IPIP_TYPE_GRE4] = &mlxsw_sp_ipip_gre4_ops,
 366};
 367