Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
0003  *
0004  * This module is not a complete tagger implementation. It only provides
0005  * primitives for taggers that rely on 802.1Q VLAN tags to use. The
0006  * dsa_8021q_netdev_ops is registered for API compliance and not used
0007  * directly by callers.
0008  */
0009 #include <linux/if_vlan.h>
0010 #include <linux/dsa/8021q.h>
0011 
0012 #include "dsa_priv.h"
0013 
0014 /* Binary structure of the fake 12-bit VID field (when the TPID is
0015  * ETH_P_DSA_8021Q):
0016  *
0017  * | 11  | 10  |  9  |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
0018  * +-----------+-----+-----------------+-----------+-----------------------+
0019  * |    RSV    | VBID|    SWITCH_ID    |   VBID    |          PORT         |
0020  * +-----------+-----+-----------------+-----------+-----------------------+
0021  *
0022  * RSV - VID[11:10]:
0023  *  Reserved. Must be set to 3 (0b11).
0024  *
0025  * SWITCH_ID - VID[8:6]:
0026  *  Index of switch within DSA tree. Must be between 0 and 7.
0027  *
0028  * VBID - { VID[9], VID[5:4] }:
0029  *  Virtual bridge ID. If between 1 and 7, packet targets the broadcast
0030  *  domain of a bridge. If transmitted as zero, packet targets a single
0031  *  port.
0032  *
0033  * PORT - VID[3:0]:
0034  *  Index of switch port. Must be between 0 and 15.
0035  */
0036 
0037 #define DSA_8021Q_RSV_VAL       3
0038 #define DSA_8021Q_RSV_SHIFT     10
0039 #define DSA_8021Q_RSV_MASK      GENMASK(11, 10)
0040 #define DSA_8021Q_RSV           ((DSA_8021Q_RSV_VAL << DSA_8021Q_RSV_SHIFT) & \
0041                                    DSA_8021Q_RSV_MASK)
0042 
0043 #define DSA_8021Q_SWITCH_ID_SHIFT   6
0044 #define DSA_8021Q_SWITCH_ID_MASK    GENMASK(8, 6)
0045 #define DSA_8021Q_SWITCH_ID(x)      (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
0046                          DSA_8021Q_SWITCH_ID_MASK)
0047 
0048 #define DSA_8021Q_VBID_HI_SHIFT     9
0049 #define DSA_8021Q_VBID_HI_MASK      GENMASK(9, 9)
0050 #define DSA_8021Q_VBID_LO_SHIFT     4
0051 #define DSA_8021Q_VBID_LO_MASK      GENMASK(5, 4)
0052 #define DSA_8021Q_VBID_HI(x)        (((x) & GENMASK(2, 2)) >> 2)
0053 #define DSA_8021Q_VBID_LO(x)        ((x) & GENMASK(1, 0))
0054 #define DSA_8021Q_VBID(x)       \
0055         (((DSA_8021Q_VBID_LO(x) << DSA_8021Q_VBID_LO_SHIFT) & \
0056           DSA_8021Q_VBID_LO_MASK) | \
0057          ((DSA_8021Q_VBID_HI(x) << DSA_8021Q_VBID_HI_SHIFT) & \
0058           DSA_8021Q_VBID_HI_MASK))
0059 
0060 #define DSA_8021Q_PORT_SHIFT        0
0061 #define DSA_8021Q_PORT_MASK     GENMASK(3, 0)
0062 #define DSA_8021Q_PORT(x)       (((x) << DSA_8021Q_PORT_SHIFT) & \
0063                          DSA_8021Q_PORT_MASK)
0064 
0065 u16 dsa_tag_8021q_bridge_vid(unsigned int bridge_num)
0066 {
0067     /* The VBID value of 0 is reserved for precise TX, but it is also
0068      * reserved/invalid for the bridge_num, so all is well.
0069      */
0070     return DSA_8021Q_RSV | DSA_8021Q_VBID(bridge_num);
0071 }
0072 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_vid);
0073 
0074 /* Returns the VID that will be installed as pvid for this switch port, sent as
0075  * tagged egress towards the CPU port and decoded by the rcv function.
0076  */
0077 u16 dsa_tag_8021q_standalone_vid(const struct dsa_port *dp)
0078 {
0079     return DSA_8021Q_RSV | DSA_8021Q_SWITCH_ID(dp->ds->index) |
0080            DSA_8021Q_PORT(dp->index);
0081 }
0082 EXPORT_SYMBOL_GPL(dsa_tag_8021q_standalone_vid);
0083 
0084 /* Returns the decoded switch ID from the RX VID. */
0085 int dsa_8021q_rx_switch_id(u16 vid)
0086 {
0087     return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
0088 }
0089 EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
0090 
0091 /* Returns the decoded port ID from the RX VID. */
0092 int dsa_8021q_rx_source_port(u16 vid)
0093 {
0094     return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
0095 }
0096 EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
0097 
0098 /* Returns the decoded VBID from the RX VID. */
0099 static int dsa_tag_8021q_rx_vbid(u16 vid)
0100 {
0101     u16 vbid_hi = (vid & DSA_8021Q_VBID_HI_MASK) >> DSA_8021Q_VBID_HI_SHIFT;
0102     u16 vbid_lo = (vid & DSA_8021Q_VBID_LO_MASK) >> DSA_8021Q_VBID_LO_SHIFT;
0103 
0104     return (vbid_hi << 2) | vbid_lo;
0105 }
0106 
0107 bool vid_is_dsa_8021q(u16 vid)
0108 {
0109     u16 rsv = (vid & DSA_8021Q_RSV_MASK) >> DSA_8021Q_RSV_SHIFT;
0110 
0111     return rsv == DSA_8021Q_RSV_VAL;
0112 }
0113 EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
0114 
0115 static struct dsa_tag_8021q_vlan *
0116 dsa_tag_8021q_vlan_find(struct dsa_8021q_context *ctx, int port, u16 vid)
0117 {
0118     struct dsa_tag_8021q_vlan *v;
0119 
0120     list_for_each_entry(v, &ctx->vlans, list)
0121         if (v->vid == vid && v->port == port)
0122             return v;
0123 
0124     return NULL;
0125 }
0126 
0127 static int dsa_port_do_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid,
0128                       u16 flags)
0129 {
0130     struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
0131     struct dsa_switch *ds = dp->ds;
0132     struct dsa_tag_8021q_vlan *v;
0133     int port = dp->index;
0134     int err;
0135 
0136     /* No need to bother with refcounting for user ports */
0137     if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
0138         return ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
0139 
0140     v = dsa_tag_8021q_vlan_find(ctx, port, vid);
0141     if (v) {
0142         refcount_inc(&v->refcount);
0143         return 0;
0144     }
0145 
0146     v = kzalloc(sizeof(*v), GFP_KERNEL);
0147     if (!v)
0148         return -ENOMEM;
0149 
0150     err = ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
0151     if (err) {
0152         kfree(v);
0153         return err;
0154     }
0155 
0156     v->vid = vid;
0157     v->port = port;
0158     refcount_set(&v->refcount, 1);
0159     list_add_tail(&v->list, &ctx->vlans);
0160 
0161     return 0;
0162 }
0163 
0164 static int dsa_port_do_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
0165 {
0166     struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
0167     struct dsa_switch *ds = dp->ds;
0168     struct dsa_tag_8021q_vlan *v;
0169     int port = dp->index;
0170     int err;
0171 
0172     /* No need to bother with refcounting for user ports */
0173     if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
0174         return ds->ops->tag_8021q_vlan_del(ds, port, vid);
0175 
0176     v = dsa_tag_8021q_vlan_find(ctx, port, vid);
0177     if (!v)
0178         return -ENOENT;
0179 
0180     if (!refcount_dec_and_test(&v->refcount))
0181         return 0;
0182 
0183     err = ds->ops->tag_8021q_vlan_del(ds, port, vid);
0184     if (err) {
0185         refcount_inc(&v->refcount);
0186         return err;
0187     }
0188 
0189     list_del(&v->list);
0190     kfree(v);
0191 
0192     return 0;
0193 }
0194 
0195 static bool
0196 dsa_port_tag_8021q_vlan_match(struct dsa_port *dp,
0197                   struct dsa_notifier_tag_8021q_vlan_info *info)
0198 {
0199     return dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp) || dp == info->dp;
0200 }
0201 
0202 int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
0203                   struct dsa_notifier_tag_8021q_vlan_info *info)
0204 {
0205     struct dsa_port *dp;
0206     int err;
0207 
0208     /* Since we use dsa_broadcast(), there might be other switches in other
0209      * trees which don't support tag_8021q, so don't return an error.
0210      * Or they might even support tag_8021q but have not registered yet to
0211      * use it (maybe they use another tagger currently).
0212      */
0213     if (!ds->ops->tag_8021q_vlan_add || !ds->tag_8021q_ctx)
0214         return 0;
0215 
0216     dsa_switch_for_each_port(dp, ds) {
0217         if (dsa_port_tag_8021q_vlan_match(dp, info)) {
0218             u16 flags = 0;
0219 
0220             if (dsa_port_is_user(dp))
0221                 flags |= BRIDGE_VLAN_INFO_UNTAGGED |
0222                      BRIDGE_VLAN_INFO_PVID;
0223 
0224             err = dsa_port_do_tag_8021q_vlan_add(dp, info->vid,
0225                                  flags);
0226             if (err)
0227                 return err;
0228         }
0229     }
0230 
0231     return 0;
0232 }
0233 
0234 int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
0235                   struct dsa_notifier_tag_8021q_vlan_info *info)
0236 {
0237     struct dsa_port *dp;
0238     int err;
0239 
0240     if (!ds->ops->tag_8021q_vlan_del || !ds->tag_8021q_ctx)
0241         return 0;
0242 
0243     dsa_switch_for_each_port(dp, ds) {
0244         if (dsa_port_tag_8021q_vlan_match(dp, info)) {
0245             err = dsa_port_do_tag_8021q_vlan_del(dp, info->vid);
0246             if (err)
0247                 return err;
0248         }
0249     }
0250 
0251     return 0;
0252 }
0253 
0254 /* There are 2 ways of offloading tag_8021q VLANs.
0255  *
0256  * One is to use a hardware TCAM to push the port's standalone VLAN into the
0257  * frame when forwarding it to the CPU, as an egress modification rule on the
0258  * CPU port. This is preferable because it has no side effects for the
0259  * autonomous forwarding path, and accomplishes tag_8021q's primary goal of
0260  * identifying the source port of each packet based on VLAN ID.
0261  *
0262  * The other is to commit the tag_8021q VLAN as a PVID to the VLAN table, and
0263  * to configure the port as VLAN-unaware. This is less preferable because
0264  * unique source port identification can only be done for standalone ports;
0265  * under a VLAN-unaware bridge, all ports share the same tag_8021q VLAN as
0266  * PVID, and under a VLAN-aware bridge, packets received by software will not
0267  * have tag_8021q VLANs appended, just bridge VLANs.
0268  *
0269  * For tag_8021q implementations of the second type, this method is used to
0270  * replace the standalone tag_8021q VLAN of a port with the tag_8021q VLAN to
0271  * be used for VLAN-unaware bridging.
0272  */
0273 int dsa_tag_8021q_bridge_join(struct dsa_switch *ds, int port,
0274                   struct dsa_bridge bridge)
0275 {
0276     struct dsa_port *dp = dsa_to_port(ds, port);
0277     u16 standalone_vid, bridge_vid;
0278     int err;
0279 
0280     /* Delete the standalone VLAN of the port and replace it with a
0281      * bridging VLAN
0282      */
0283     standalone_vid = dsa_tag_8021q_standalone_vid(dp);
0284     bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
0285 
0286     err = dsa_port_tag_8021q_vlan_add(dp, bridge_vid, true);
0287     if (err)
0288         return err;
0289 
0290     dsa_port_tag_8021q_vlan_del(dp, standalone_vid, false);
0291 
0292     return 0;
0293 }
0294 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_join);
0295 
0296 void dsa_tag_8021q_bridge_leave(struct dsa_switch *ds, int port,
0297                 struct dsa_bridge bridge)
0298 {
0299     struct dsa_port *dp = dsa_to_port(ds, port);
0300     u16 standalone_vid, bridge_vid;
0301     int err;
0302 
0303     /* Delete the bridging VLAN of the port and replace it with a
0304      * standalone VLAN
0305      */
0306     standalone_vid = dsa_tag_8021q_standalone_vid(dp);
0307     bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
0308 
0309     err = dsa_port_tag_8021q_vlan_add(dp, standalone_vid, false);
0310     if (err) {
0311         dev_err(ds->dev,
0312             "Failed to delete tag_8021q standalone VLAN %d from port %d: %pe\n",
0313             standalone_vid, port, ERR_PTR(err));
0314     }
0315 
0316     dsa_port_tag_8021q_vlan_del(dp, bridge_vid, true);
0317 }
0318 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_leave);
0319 
0320 /* Set up a port's standalone tag_8021q VLAN */
0321 static int dsa_tag_8021q_port_setup(struct dsa_switch *ds, int port)
0322 {
0323     struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0324     struct dsa_port *dp = dsa_to_port(ds, port);
0325     u16 vid = dsa_tag_8021q_standalone_vid(dp);
0326     struct net_device *master;
0327     int err;
0328 
0329     /* The CPU port is implicitly configured by
0330      * configuring the front-panel ports
0331      */
0332     if (!dsa_port_is_user(dp))
0333         return 0;
0334 
0335     master = dp->cpu_dp->master;
0336 
0337     err = dsa_port_tag_8021q_vlan_add(dp, vid, false);
0338     if (err) {
0339         dev_err(ds->dev,
0340             "Failed to apply standalone VID %d to port %d: %pe\n",
0341             vid, port, ERR_PTR(err));
0342         return err;
0343     }
0344 
0345     /* Add the VLAN to the master's RX filter. */
0346     vlan_vid_add(master, ctx->proto, vid);
0347 
0348     return err;
0349 }
0350 
0351 static void dsa_tag_8021q_port_teardown(struct dsa_switch *ds, int port)
0352 {
0353     struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0354     struct dsa_port *dp = dsa_to_port(ds, port);
0355     u16 vid = dsa_tag_8021q_standalone_vid(dp);
0356     struct net_device *master;
0357 
0358     /* The CPU port is implicitly configured by
0359      * configuring the front-panel ports
0360      */
0361     if (!dsa_port_is_user(dp))
0362         return;
0363 
0364     master = dp->cpu_dp->master;
0365 
0366     dsa_port_tag_8021q_vlan_del(dp, vid, false);
0367 
0368     vlan_vid_del(master, ctx->proto, vid);
0369 }
0370 
0371 static int dsa_tag_8021q_setup(struct dsa_switch *ds)
0372 {
0373     int err, port;
0374 
0375     ASSERT_RTNL();
0376 
0377     for (port = 0; port < ds->num_ports; port++) {
0378         err = dsa_tag_8021q_port_setup(ds, port);
0379         if (err < 0) {
0380             dev_err(ds->dev,
0381                 "Failed to setup VLAN tagging for port %d: %pe\n",
0382                 port, ERR_PTR(err));
0383             return err;
0384         }
0385     }
0386 
0387     return 0;
0388 }
0389 
0390 static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
0391 {
0392     int port;
0393 
0394     ASSERT_RTNL();
0395 
0396     for (port = 0; port < ds->num_ports; port++)
0397         dsa_tag_8021q_port_teardown(ds, port);
0398 }
0399 
0400 int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
0401 {
0402     struct dsa_8021q_context *ctx;
0403 
0404     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0405     if (!ctx)
0406         return -ENOMEM;
0407 
0408     ctx->proto = proto;
0409     ctx->ds = ds;
0410 
0411     INIT_LIST_HEAD(&ctx->vlans);
0412 
0413     ds->tag_8021q_ctx = ctx;
0414 
0415     return dsa_tag_8021q_setup(ds);
0416 }
0417 EXPORT_SYMBOL_GPL(dsa_tag_8021q_register);
0418 
0419 void dsa_tag_8021q_unregister(struct dsa_switch *ds)
0420 {
0421     struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0422     struct dsa_tag_8021q_vlan *v, *n;
0423 
0424     dsa_tag_8021q_teardown(ds);
0425 
0426     list_for_each_entry_safe(v, n, &ctx->vlans, list) {
0427         list_del(&v->list);
0428         kfree(v);
0429     }
0430 
0431     ds->tag_8021q_ctx = NULL;
0432 
0433     kfree(ctx);
0434 }
0435 EXPORT_SYMBOL_GPL(dsa_tag_8021q_unregister);
0436 
0437 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
0438                    u16 tpid, u16 tci)
0439 {
0440     /* skb->data points at skb_mac_header, which
0441      * is fine for vlan_insert_tag.
0442      */
0443     return vlan_insert_tag(skb, htons(tpid), tci);
0444 }
0445 EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
0446 
0447 struct net_device *dsa_tag_8021q_find_port_by_vbid(struct net_device *master,
0448                            int vbid)
0449 {
0450     struct dsa_port *cpu_dp = master->dsa_ptr;
0451     struct dsa_switch_tree *dst = cpu_dp->dst;
0452     struct dsa_port *dp;
0453 
0454     if (WARN_ON(!vbid))
0455         return NULL;
0456 
0457     dsa_tree_for_each_user_port(dp, dst) {
0458         if (!dp->bridge)
0459             continue;
0460 
0461         if (dp->stp_state != BR_STATE_LEARNING &&
0462             dp->stp_state != BR_STATE_FORWARDING)
0463             continue;
0464 
0465         if (dp->cpu_dp != cpu_dp)
0466             continue;
0467 
0468         if (dsa_port_bridge_num_get(dp) == vbid)
0469             return dp->slave;
0470     }
0471 
0472     return NULL;
0473 }
0474 EXPORT_SYMBOL_GPL(dsa_tag_8021q_find_port_by_vbid);
0475 
0476 void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id,
0477            int *vbid)
0478 {
0479     u16 vid, tci;
0480 
0481     if (skb_vlan_tag_present(skb)) {
0482         tci = skb_vlan_tag_get(skb);
0483         __vlan_hwaccel_clear_tag(skb);
0484     } else {
0485         skb_push_rcsum(skb, ETH_HLEN);
0486         __skb_vlan_pop(skb, &tci);
0487         skb_pull_rcsum(skb, ETH_HLEN);
0488     }
0489 
0490     vid = tci & VLAN_VID_MASK;
0491 
0492     *source_port = dsa_8021q_rx_source_port(vid);
0493     *switch_id = dsa_8021q_rx_switch_id(vid);
0494 
0495     if (vbid)
0496         *vbid = dsa_tag_8021q_rx_vbid(vid);
0497 
0498     skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
0499 }
0500 EXPORT_SYMBOL_GPL(dsa_8021q_rcv);