Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Xenbus code for netif backend
0004  *
0005  * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
0006  * Copyright (C) 2005 XenSource Ltd
0007 */
0008 
0009 #include "common.h"
0010 #include <linux/vmalloc.h>
0011 #include <linux/rtnetlink.h>
0012 
0013 static int connect_data_rings(struct backend_info *be,
0014                   struct xenvif_queue *queue);
0015 static void connect(struct backend_info *be);
0016 static int read_xenbus_vif_flags(struct backend_info *be);
0017 static int backend_create_xenvif(struct backend_info *be);
0018 static void unregister_hotplug_status_watch(struct backend_info *be);
0019 static void xen_unregister_watchers(struct xenvif *vif);
0020 static void set_backend_state(struct backend_info *be,
0021                   enum xenbus_state state);
0022 
0023 #ifdef CONFIG_DEBUG_FS
0024 struct dentry *xen_netback_dbg_root = NULL;
0025 
0026 static int xenvif_read_io_ring(struct seq_file *m, void *v)
0027 {
0028     struct xenvif_queue *queue = m->private;
0029     struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
0030     struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
0031     struct netdev_queue *dev_queue;
0032 
0033     if (tx_ring->sring) {
0034         struct xen_netif_tx_sring *sring = tx_ring->sring;
0035 
0036         seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
0037                tx_ring->nr_ents);
0038         seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
0039                sring->req_prod,
0040                sring->req_prod - sring->rsp_prod,
0041                tx_ring->req_cons,
0042                tx_ring->req_cons - sring->rsp_prod,
0043                sring->req_event,
0044                sring->req_event - sring->rsp_prod);
0045         seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
0046                sring->rsp_prod,
0047                tx_ring->rsp_prod_pvt,
0048                tx_ring->rsp_prod_pvt - sring->rsp_prod,
0049                sring->rsp_event,
0050                sring->rsp_event - sring->rsp_prod);
0051         seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
0052                queue->pending_prod,
0053                queue->pending_cons,
0054                nr_pending_reqs(queue));
0055         seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
0056                queue->dealloc_prod,
0057                queue->dealloc_cons,
0058                queue->dealloc_prod - queue->dealloc_cons);
0059     }
0060 
0061     if (rx_ring->sring) {
0062         struct xen_netif_rx_sring *sring = rx_ring->sring;
0063 
0064         seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
0065         seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
0066                sring->req_prod,
0067                sring->req_prod - sring->rsp_prod,
0068                rx_ring->req_cons,
0069                rx_ring->req_cons - sring->rsp_prod,
0070                sring->req_event,
0071                sring->req_event - sring->rsp_prod);
0072         seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
0073                sring->rsp_prod,
0074                rx_ring->rsp_prod_pvt,
0075                rx_ring->rsp_prod_pvt - sring->rsp_prod,
0076                sring->rsp_event,
0077                sring->rsp_event - sring->rsp_prod);
0078     }
0079 
0080     seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
0081            "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
0082            "remaining: %lu, expires: %lu, now: %lu\n",
0083            queue->napi.state, queue->napi.weight,
0084            skb_queue_len(&queue->tx_queue),
0085            timer_pending(&queue->credit_timeout),
0086            queue->credit_bytes,
0087            queue->credit_usec,
0088            queue->remaining_credit,
0089            queue->credit_timeout.expires,
0090            jiffies);
0091 
0092     dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
0093 
0094     seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
0095            queue->rx_queue_len, queue->rx_queue_max,
0096            skb_queue_len(&queue->rx_queue),
0097            netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
0098 
0099     return 0;
0100 }
0101 
0102 #define XENVIF_KICK_STR "kick"
0103 #define BUFFER_SIZE     32
0104 
0105 static ssize_t
0106 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
0107              loff_t *ppos)
0108 {
0109     struct xenvif_queue *queue =
0110         ((struct seq_file *)filp->private_data)->private;
0111     int len;
0112     char write[BUFFER_SIZE];
0113 
0114     /* don't allow partial writes and check the length */
0115     if (*ppos != 0)
0116         return 0;
0117     if (count >= sizeof(write))
0118         return -ENOSPC;
0119 
0120     len = simple_write_to_buffer(write,
0121                      sizeof(write) - 1,
0122                      ppos,
0123                      buf,
0124                      count);
0125     if (len < 0)
0126         return len;
0127 
0128     write[len] = '\0';
0129 
0130     if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
0131         xenvif_interrupt(0, (void *)queue);
0132     else {
0133         pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
0134             queue->id);
0135         count = -EINVAL;
0136     }
0137     return count;
0138 }
0139 
0140 static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
0141 {
0142     int ret;
0143     void *queue = NULL;
0144 
0145     if (inode->i_private)
0146         queue = inode->i_private;
0147     ret = single_open(filp, xenvif_read_io_ring, queue);
0148     filp->f_mode |= FMODE_PWRITE;
0149     return ret;
0150 }
0151 
0152 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
0153     .owner = THIS_MODULE,
0154     .open = xenvif_io_ring_open,
0155     .read = seq_read,
0156     .llseek = seq_lseek,
0157     .release = single_release,
0158     .write = xenvif_write_io_ring,
0159 };
0160 
0161 static int xenvif_ctrl_show(struct seq_file *m, void *v)
0162 {
0163     struct xenvif *vif = m->private;
0164 
0165     xenvif_dump_hash_info(vif, m);
0166 
0167     return 0;
0168 }
0169 DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
0170 
0171 static void xenvif_debugfs_addif(struct xenvif *vif)
0172 {
0173     int i;
0174 
0175     vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
0176                           xen_netback_dbg_root);
0177     for (i = 0; i < vif->num_queues; ++i) {
0178         char filename[sizeof("io_ring_q") + 4];
0179 
0180         snprintf(filename, sizeof(filename), "io_ring_q%d", i);
0181         debugfs_create_file(filename, 0600, vif->xenvif_dbg_root,
0182                     &vif->queues[i],
0183                     &xenvif_dbg_io_ring_ops_fops);
0184     }
0185 
0186     if (vif->ctrl_irq)
0187         debugfs_create_file("ctrl", 0400, vif->xenvif_dbg_root, vif,
0188                     &xenvif_ctrl_fops);
0189 }
0190 
0191 static void xenvif_debugfs_delif(struct xenvif *vif)
0192 {
0193     debugfs_remove_recursive(vif->xenvif_dbg_root);
0194     vif->xenvif_dbg_root = NULL;
0195 }
0196 #endif /* CONFIG_DEBUG_FS */
0197 
0198 /*
0199  * Handle the creation of the hotplug script environment.  We add the script
0200  * and vif variables to the environment, for the benefit of the vif-* hotplug
0201  * scripts.
0202  */
0203 static int netback_uevent(struct xenbus_device *xdev,
0204               struct kobj_uevent_env *env)
0205 {
0206     struct backend_info *be = dev_get_drvdata(&xdev->dev);
0207 
0208     if (!be)
0209         return 0;
0210 
0211     if (add_uevent_var(env, "script=%s", be->hotplug_script))
0212         return -ENOMEM;
0213 
0214     if (!be->vif)
0215         return 0;
0216 
0217     return add_uevent_var(env, "vif=%s", be->vif->dev->name);
0218 }
0219 
0220 
0221 static int backend_create_xenvif(struct backend_info *be)
0222 {
0223     int err;
0224     long handle;
0225     struct xenbus_device *dev = be->dev;
0226     struct xenvif *vif;
0227 
0228     if (be->vif != NULL)
0229         return 0;
0230 
0231     err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
0232     if (err != 1) {
0233         xenbus_dev_fatal(dev, err, "reading handle");
0234         return (err < 0) ? err : -EINVAL;
0235     }
0236 
0237     vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
0238     if (IS_ERR(vif)) {
0239         err = PTR_ERR(vif);
0240         xenbus_dev_fatal(dev, err, "creating interface");
0241         return err;
0242     }
0243     be->vif = vif;
0244     vif->be = be;
0245 
0246     kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
0247     return 0;
0248 }
0249 
0250 static void backend_disconnect(struct backend_info *be)
0251 {
0252     struct xenvif *vif = be->vif;
0253 
0254     if (vif) {
0255         unsigned int num_queues = vif->num_queues;
0256         unsigned int queue_index;
0257 
0258         xen_unregister_watchers(vif);
0259 #ifdef CONFIG_DEBUG_FS
0260         xenvif_debugfs_delif(vif);
0261 #endif /* CONFIG_DEBUG_FS */
0262         xenvif_disconnect_data(vif);
0263 
0264         /* At this point some of the handlers may still be active
0265          * so we need to have additional synchronization here.
0266          */
0267         vif->num_queues = 0;
0268         synchronize_net();
0269 
0270         for (queue_index = 0; queue_index < num_queues; ++queue_index)
0271             xenvif_deinit_queue(&vif->queues[queue_index]);
0272 
0273         vfree(vif->queues);
0274         vif->queues = NULL;
0275 
0276         xenvif_disconnect_ctrl(vif);
0277     }
0278 }
0279 
0280 static void backend_connect(struct backend_info *be)
0281 {
0282     if (be->vif)
0283         connect(be);
0284 }
0285 
0286 static inline void backend_switch_state(struct backend_info *be,
0287                     enum xenbus_state state)
0288 {
0289     struct xenbus_device *dev = be->dev;
0290 
0291     pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
0292     be->state = state;
0293 
0294     /* If we are waiting for a hotplug script then defer the
0295      * actual xenbus state change.
0296      */
0297     if (!be->have_hotplug_status_watch)
0298         xenbus_switch_state(dev, state);
0299 }
0300 
0301 /* Handle backend state transitions:
0302  *
0303  * The backend state starts in Initialising and the following transitions are
0304  * allowed.
0305  *
0306  * Initialising -> InitWait -> Connected
0307  *          \
0308  *           \        ^    \         |
0309  *            \       |     \        |
0310  *             \      |      \       |
0311  *              \     |       \      |
0312  *               \    |        \     |
0313  *                \   |         \    |
0314  *                 V  |          V   V
0315  *
0316  *                  Closed  <-> Closing
0317  *
0318  * The state argument specifies the eventual state of the backend and the
0319  * function transitions to that state via the shortest path.
0320  */
0321 static void set_backend_state(struct backend_info *be,
0322                   enum xenbus_state state)
0323 {
0324     while (be->state != state) {
0325         switch (be->state) {
0326         case XenbusStateInitialising:
0327             switch (state) {
0328             case XenbusStateInitWait:
0329             case XenbusStateConnected:
0330             case XenbusStateClosing:
0331                 backend_switch_state(be, XenbusStateInitWait);
0332                 break;
0333             case XenbusStateClosed:
0334                 backend_switch_state(be, XenbusStateClosed);
0335                 break;
0336             default:
0337                 BUG();
0338             }
0339             break;
0340         case XenbusStateClosed:
0341             switch (state) {
0342             case XenbusStateInitWait:
0343             case XenbusStateConnected:
0344                 backend_switch_state(be, XenbusStateInitWait);
0345                 break;
0346             case XenbusStateClosing:
0347                 backend_switch_state(be, XenbusStateClosing);
0348                 break;
0349             default:
0350                 BUG();
0351             }
0352             break;
0353         case XenbusStateInitWait:
0354             switch (state) {
0355             case XenbusStateConnected:
0356                 backend_connect(be);
0357                 backend_switch_state(be, XenbusStateConnected);
0358                 break;
0359             case XenbusStateClosing:
0360             case XenbusStateClosed:
0361                 backend_switch_state(be, XenbusStateClosing);
0362                 break;
0363             default:
0364                 BUG();
0365             }
0366             break;
0367         case XenbusStateConnected:
0368             switch (state) {
0369             case XenbusStateInitWait:
0370             case XenbusStateClosing:
0371             case XenbusStateClosed:
0372                 backend_disconnect(be);
0373                 backend_switch_state(be, XenbusStateClosing);
0374                 break;
0375             default:
0376                 BUG();
0377             }
0378             break;
0379         case XenbusStateClosing:
0380             switch (state) {
0381             case XenbusStateInitWait:
0382             case XenbusStateConnected:
0383             case XenbusStateClosed:
0384                 backend_switch_state(be, XenbusStateClosed);
0385                 break;
0386             default:
0387                 BUG();
0388             }
0389             break;
0390         default:
0391             BUG();
0392         }
0393     }
0394 }
0395 
0396 static void read_xenbus_frontend_xdp(struct backend_info *be,
0397                       struct xenbus_device *dev)
0398 {
0399     struct xenvif *vif = be->vif;
0400     u16 headroom;
0401     int err;
0402 
0403     err = xenbus_scanf(XBT_NIL, dev->otherend,
0404                "xdp-headroom", "%hu", &headroom);
0405     if (err != 1) {
0406         vif->xdp_headroom = 0;
0407         return;
0408     }
0409     if (headroom > XEN_NETIF_MAX_XDP_HEADROOM)
0410         headroom = XEN_NETIF_MAX_XDP_HEADROOM;
0411     vif->xdp_headroom = headroom;
0412 }
0413 
0414 /*
0415  * Callback received when the frontend's state changes.
0416  */
0417 static void frontend_changed(struct xenbus_device *dev,
0418                  enum xenbus_state frontend_state)
0419 {
0420     struct backend_info *be = dev_get_drvdata(&dev->dev);
0421 
0422     pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
0423 
0424     be->frontend_state = frontend_state;
0425 
0426     switch (frontend_state) {
0427     case XenbusStateInitialising:
0428         set_backend_state(be, XenbusStateInitWait);
0429         break;
0430 
0431     case XenbusStateInitialised:
0432         break;
0433 
0434     case XenbusStateConnected:
0435         set_backend_state(be, XenbusStateConnected);
0436         break;
0437 
0438     case XenbusStateReconfiguring:
0439         read_xenbus_frontend_xdp(be, dev);
0440         xenbus_switch_state(dev, XenbusStateReconfigured);
0441         break;
0442 
0443     case XenbusStateClosing:
0444         set_backend_state(be, XenbusStateClosing);
0445         break;
0446 
0447     case XenbusStateClosed:
0448         set_backend_state(be, XenbusStateClosed);
0449         if (xenbus_dev_is_online(dev))
0450             break;
0451         fallthrough;    /* if not online */
0452     case XenbusStateUnknown:
0453         set_backend_state(be, XenbusStateClosed);
0454         device_unregister(&dev->dev);
0455         break;
0456 
0457     default:
0458         xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
0459                  frontend_state);
0460         break;
0461     }
0462 }
0463 
0464 
0465 static void xen_net_read_rate(struct xenbus_device *dev,
0466                   unsigned long *bytes, unsigned long *usec)
0467 {
0468     char *s, *e;
0469     unsigned long b, u;
0470     char *ratestr;
0471 
0472     /* Default to unlimited bandwidth. */
0473     *bytes = ~0UL;
0474     *usec = 0;
0475 
0476     ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
0477     if (IS_ERR(ratestr))
0478         return;
0479 
0480     s = ratestr;
0481     b = simple_strtoul(s, &e, 10);
0482     if ((s == e) || (*e != ','))
0483         goto fail;
0484 
0485     s = e + 1;
0486     u = simple_strtoul(s, &e, 10);
0487     if ((s == e) || (*e != '\0'))
0488         goto fail;
0489 
0490     *bytes = b;
0491     *usec = u;
0492 
0493     kfree(ratestr);
0494     return;
0495 
0496  fail:
0497     pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
0498     kfree(ratestr);
0499 }
0500 
0501 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
0502 {
0503     char *s, *e, *macstr;
0504     int i;
0505 
0506     macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
0507     if (IS_ERR(macstr))
0508         return PTR_ERR(macstr);
0509 
0510     for (i = 0; i < ETH_ALEN; i++) {
0511         mac[i] = simple_strtoul(s, &e, 16);
0512         if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
0513             kfree(macstr);
0514             return -ENOENT;
0515         }
0516         s = e+1;
0517     }
0518 
0519     kfree(macstr);
0520     return 0;
0521 }
0522 
0523 static void xen_net_rate_changed(struct xenbus_watch *watch,
0524                  const char *path, const char *token)
0525 {
0526     struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
0527     struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
0528     unsigned long   credit_bytes;
0529     unsigned long   credit_usec;
0530     unsigned int queue_index;
0531 
0532     xen_net_read_rate(dev, &credit_bytes, &credit_usec);
0533     for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
0534         struct xenvif_queue *queue = &vif->queues[queue_index];
0535 
0536         queue->credit_bytes = credit_bytes;
0537         queue->credit_usec = credit_usec;
0538         if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
0539             queue->remaining_credit > queue->credit_bytes) {
0540             queue->remaining_credit = queue->credit_bytes;
0541         }
0542     }
0543 }
0544 
0545 static int xen_register_credit_watch(struct xenbus_device *dev,
0546                      struct xenvif *vif)
0547 {
0548     int err = 0;
0549     char *node;
0550     unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
0551 
0552     if (vif->credit_watch.node)
0553         return -EADDRINUSE;
0554 
0555     node = kmalloc(maxlen, GFP_KERNEL);
0556     if (!node)
0557         return -ENOMEM;
0558     snprintf(node, maxlen, "%s/rate", dev->nodename);
0559     vif->credit_watch.node = node;
0560     vif->credit_watch.will_handle = NULL;
0561     vif->credit_watch.callback = xen_net_rate_changed;
0562     err = register_xenbus_watch(&vif->credit_watch);
0563     if (err) {
0564         pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
0565         kfree(node);
0566         vif->credit_watch.node = NULL;
0567         vif->credit_watch.will_handle = NULL;
0568         vif->credit_watch.callback = NULL;
0569     }
0570     return err;
0571 }
0572 
0573 static void xen_unregister_credit_watch(struct xenvif *vif)
0574 {
0575     if (vif->credit_watch.node) {
0576         unregister_xenbus_watch(&vif->credit_watch);
0577         kfree(vif->credit_watch.node);
0578         vif->credit_watch.node = NULL;
0579     }
0580 }
0581 
0582 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
0583                    const char *path, const char *token)
0584 {
0585     struct xenvif *vif = container_of(watch, struct xenvif,
0586                       mcast_ctrl_watch);
0587     struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
0588 
0589     vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
0590                     "request-multicast-control", 0);
0591 }
0592 
0593 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
0594                      struct xenvif *vif)
0595 {
0596     int err = 0;
0597     char *node;
0598     unsigned maxlen = strlen(dev->otherend) +
0599         sizeof("/request-multicast-control");
0600 
0601     if (vif->mcast_ctrl_watch.node) {
0602         pr_err_ratelimited("Watch is already registered\n");
0603         return -EADDRINUSE;
0604     }
0605 
0606     node = kmalloc(maxlen, GFP_KERNEL);
0607     if (!node) {
0608         pr_err("Failed to allocate memory for watch\n");
0609         return -ENOMEM;
0610     }
0611     snprintf(node, maxlen, "%s/request-multicast-control",
0612          dev->otherend);
0613     vif->mcast_ctrl_watch.node = node;
0614     vif->mcast_ctrl_watch.will_handle = NULL;
0615     vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
0616     err = register_xenbus_watch(&vif->mcast_ctrl_watch);
0617     if (err) {
0618         pr_err("Failed to set watcher %s\n",
0619                vif->mcast_ctrl_watch.node);
0620         kfree(node);
0621         vif->mcast_ctrl_watch.node = NULL;
0622         vif->mcast_ctrl_watch.will_handle = NULL;
0623         vif->mcast_ctrl_watch.callback = NULL;
0624     }
0625     return err;
0626 }
0627 
0628 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
0629 {
0630     if (vif->mcast_ctrl_watch.node) {
0631         unregister_xenbus_watch(&vif->mcast_ctrl_watch);
0632         kfree(vif->mcast_ctrl_watch.node);
0633         vif->mcast_ctrl_watch.node = NULL;
0634     }
0635 }
0636 
0637 static void xen_register_watchers(struct xenbus_device *dev,
0638                   struct xenvif *vif)
0639 {
0640     xen_register_credit_watch(dev, vif);
0641     xen_register_mcast_ctrl_watch(dev, vif);
0642 }
0643 
0644 static void xen_unregister_watchers(struct xenvif *vif)
0645 {
0646     xen_unregister_mcast_ctrl_watch(vif);
0647     xen_unregister_credit_watch(vif);
0648 }
0649 
0650 static void unregister_hotplug_status_watch(struct backend_info *be)
0651 {
0652     if (be->have_hotplug_status_watch) {
0653         unregister_xenbus_watch(&be->hotplug_status_watch);
0654         kfree(be->hotplug_status_watch.node);
0655     }
0656     be->have_hotplug_status_watch = 0;
0657 }
0658 
0659 static void hotplug_status_changed(struct xenbus_watch *watch,
0660                    const char *path,
0661                    const char *token)
0662 {
0663     struct backend_info *be = container_of(watch,
0664                            struct backend_info,
0665                            hotplug_status_watch);
0666     char *str;
0667     unsigned int len;
0668 
0669     str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
0670     if (IS_ERR(str))
0671         return;
0672     if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
0673         /* Complete any pending state change */
0674         xenbus_switch_state(be->dev, be->state);
0675 
0676         /* Not interested in this watch anymore. */
0677         unregister_hotplug_status_watch(be);
0678     }
0679     kfree(str);
0680 }
0681 
0682 static int connect_ctrl_ring(struct backend_info *be)
0683 {
0684     struct xenbus_device *dev = be->dev;
0685     struct xenvif *vif = be->vif;
0686     unsigned int val;
0687     grant_ref_t ring_ref;
0688     unsigned int evtchn;
0689     int err;
0690 
0691     err = xenbus_scanf(XBT_NIL, dev->otherend,
0692                "ctrl-ring-ref", "%u", &val);
0693     if (err < 0)
0694         goto done; /* The frontend does not have a control ring */
0695 
0696     ring_ref = val;
0697 
0698     err = xenbus_scanf(XBT_NIL, dev->otherend,
0699                "event-channel-ctrl", "%u", &val);
0700     if (err < 0) {
0701         xenbus_dev_fatal(dev, err,
0702                  "reading %s/event-channel-ctrl",
0703                  dev->otherend);
0704         goto fail;
0705     }
0706 
0707     evtchn = val;
0708 
0709     err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
0710     if (err) {
0711         xenbus_dev_fatal(dev, err,
0712                  "mapping shared-frame %u port %u",
0713                  ring_ref, evtchn);
0714         goto fail;
0715     }
0716 
0717 done:
0718     return 0;
0719 
0720 fail:
0721     return err;
0722 }
0723 
0724 static void connect(struct backend_info *be)
0725 {
0726     int err;
0727     struct xenbus_device *dev = be->dev;
0728     unsigned long credit_bytes, credit_usec;
0729     unsigned int queue_index;
0730     unsigned int requested_num_queues;
0731     struct xenvif_queue *queue;
0732 
0733     /* Check whether the frontend requested multiple queues
0734      * and read the number requested.
0735      */
0736     requested_num_queues = xenbus_read_unsigned(dev->otherend,
0737                     "multi-queue-num-queues", 1);
0738     if (requested_num_queues > xenvif_max_queues) {
0739         /* buggy or malicious guest */
0740         xenbus_dev_fatal(dev, -EINVAL,
0741                  "guest requested %u queues, exceeding the maximum of %u.",
0742                  requested_num_queues, xenvif_max_queues);
0743         return;
0744     }
0745 
0746     err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
0747     if (err) {
0748         xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
0749         return;
0750     }
0751 
0752     xen_net_read_rate(dev, &credit_bytes, &credit_usec);
0753     xen_unregister_watchers(be->vif);
0754     xen_register_watchers(dev, be->vif);
0755     read_xenbus_vif_flags(be);
0756 
0757     err = connect_ctrl_ring(be);
0758     if (err) {
0759         xenbus_dev_fatal(dev, err, "connecting control ring");
0760         return;
0761     }
0762 
0763     /* Use the number of queues requested by the frontend */
0764     be->vif->queues = vzalloc(array_size(requested_num_queues,
0765                          sizeof(struct xenvif_queue)));
0766     if (!be->vif->queues) {
0767         xenbus_dev_fatal(dev, -ENOMEM,
0768                  "allocating queues");
0769         return;
0770     }
0771 
0772     be->vif->num_queues = requested_num_queues;
0773     be->vif->stalled_queues = requested_num_queues;
0774 
0775     for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
0776         queue = &be->vif->queues[queue_index];
0777         queue->vif = be->vif;
0778         queue->id = queue_index;
0779         snprintf(queue->name, sizeof(queue->name), "%s-q%u",
0780                 be->vif->dev->name, queue->id);
0781 
0782         err = xenvif_init_queue(queue);
0783         if (err) {
0784             /* xenvif_init_queue() cleans up after itself on
0785              * failure, but we need to clean up any previously
0786              * initialised queues. Set num_queues to i so that
0787              * earlier queues can be destroyed using the regular
0788              * disconnect logic.
0789              */
0790             be->vif->num_queues = queue_index;
0791             goto err;
0792         }
0793 
0794         queue->credit_bytes = credit_bytes;
0795         queue->remaining_credit = credit_bytes;
0796         queue->credit_usec = credit_usec;
0797 
0798         err = connect_data_rings(be, queue);
0799         if (err) {
0800             /* connect_data_rings() cleans up after itself on
0801              * failure, but we need to clean up after
0802              * xenvif_init_queue() here, and also clean up any
0803              * previously initialised queues.
0804              */
0805             xenvif_deinit_queue(queue);
0806             be->vif->num_queues = queue_index;
0807             goto err;
0808         }
0809     }
0810 
0811 #ifdef CONFIG_DEBUG_FS
0812     xenvif_debugfs_addif(be->vif);
0813 #endif /* CONFIG_DEBUG_FS */
0814 
0815     /* Initialisation completed, tell core driver the number of
0816      * active queues.
0817      */
0818     rtnl_lock();
0819     netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
0820     netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
0821     rtnl_unlock();
0822 
0823     xenvif_carrier_on(be->vif);
0824 
0825     unregister_hotplug_status_watch(be);
0826     err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, NULL,
0827                    hotplug_status_changed,
0828                    "%s/%s", dev->nodename, "hotplug-status");
0829     if (!err)
0830         be->have_hotplug_status_watch = 1;
0831 
0832     netif_tx_wake_all_queues(be->vif->dev);
0833 
0834     return;
0835 
0836 err:
0837     if (be->vif->num_queues > 0)
0838         xenvif_disconnect_data(be->vif); /* Clean up existing queues */
0839     for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
0840         xenvif_deinit_queue(&be->vif->queues[queue_index]);
0841     vfree(be->vif->queues);
0842     be->vif->queues = NULL;
0843     be->vif->num_queues = 0;
0844     xenvif_disconnect_ctrl(be->vif);
0845     return;
0846 }
0847 
0848 
0849 static int connect_data_rings(struct backend_info *be,
0850                   struct xenvif_queue *queue)
0851 {
0852     struct xenbus_device *dev = be->dev;
0853     unsigned int num_queues = queue->vif->num_queues;
0854     unsigned long tx_ring_ref, rx_ring_ref;
0855     unsigned int tx_evtchn, rx_evtchn;
0856     int err;
0857     char *xspath;
0858     size_t xspathsize;
0859     const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
0860 
0861     /* If the frontend requested 1 queue, or we have fallen back
0862      * to single queue due to lack of frontend support for multi-
0863      * queue, expect the remaining XenStore keys in the toplevel
0864      * directory. Otherwise, expect them in a subdirectory called
0865      * queue-N.
0866      */
0867     if (num_queues == 1) {
0868         xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
0869         if (!xspath) {
0870             xenbus_dev_fatal(dev, -ENOMEM,
0871                      "reading ring references");
0872             return -ENOMEM;
0873         }
0874         strcpy(xspath, dev->otherend);
0875     } else {
0876         xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
0877         xspath = kzalloc(xspathsize, GFP_KERNEL);
0878         if (!xspath) {
0879             xenbus_dev_fatal(dev, -ENOMEM,
0880                      "reading ring references");
0881             return -ENOMEM;
0882         }
0883         snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
0884              queue->id);
0885     }
0886 
0887     err = xenbus_gather(XBT_NIL, xspath,
0888                 "tx-ring-ref", "%lu", &tx_ring_ref,
0889                 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
0890     if (err) {
0891         xenbus_dev_fatal(dev, err,
0892                  "reading %s/ring-ref",
0893                  xspath);
0894         goto err;
0895     }
0896 
0897     /* Try split event channels first, then single event channel. */
0898     err = xenbus_gather(XBT_NIL, xspath,
0899                 "event-channel-tx", "%u", &tx_evtchn,
0900                 "event-channel-rx", "%u", &rx_evtchn, NULL);
0901     if (err < 0) {
0902         err = xenbus_scanf(XBT_NIL, xspath,
0903                    "event-channel", "%u", &tx_evtchn);
0904         if (err < 0) {
0905             xenbus_dev_fatal(dev, err,
0906                      "reading %s/event-channel(-tx/rx)",
0907                      xspath);
0908             goto err;
0909         }
0910         rx_evtchn = tx_evtchn;
0911     }
0912 
0913     /* Map the shared frame, irq etc. */
0914     err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
0915                   tx_evtchn, rx_evtchn);
0916     if (err) {
0917         xenbus_dev_fatal(dev, err,
0918                  "mapping shared-frames %lu/%lu port tx %u rx %u",
0919                  tx_ring_ref, rx_ring_ref,
0920                  tx_evtchn, rx_evtchn);
0921         goto err;
0922     }
0923 
0924     err = 0;
0925 err: /* Regular return falls through with err == 0 */
0926     kfree(xspath);
0927     return err;
0928 }
0929 
0930 static int read_xenbus_vif_flags(struct backend_info *be)
0931 {
0932     struct xenvif *vif = be->vif;
0933     struct xenbus_device *dev = be->dev;
0934     unsigned int rx_copy;
0935     int err;
0936 
0937     err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
0938                &rx_copy);
0939     if (err == -ENOENT) {
0940         err = 0;
0941         rx_copy = 0;
0942     }
0943     if (err < 0) {
0944         xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
0945                  dev->otherend);
0946         return err;
0947     }
0948     if (!rx_copy)
0949         return -EOPNOTSUPP;
0950 
0951     if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
0952         /* - Reduce drain timeout to poll more frequently for
0953          *   Rx requests.
0954          * - Disable Rx stall detection.
0955          */
0956         be->vif->drain_timeout = msecs_to_jiffies(30);
0957         be->vif->stall_timeout = 0;
0958     }
0959 
0960     vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
0961 
0962     vif->gso_mask = 0;
0963 
0964     if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
0965         vif->gso_mask |= GSO_BIT(TCPV4);
0966 
0967     if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
0968         vif->gso_mask |= GSO_BIT(TCPV6);
0969 
0970     vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
0971                          "feature-no-csum-offload", 0);
0972 
0973     vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
0974                         "feature-ipv6-csum-offload", 0);
0975 
0976     read_xenbus_frontend_xdp(be, dev);
0977 
0978     return 0;
0979 }
0980 
0981 static int netback_remove(struct xenbus_device *dev)
0982 {
0983     struct backend_info *be = dev_get_drvdata(&dev->dev);
0984 
0985     unregister_hotplug_status_watch(be);
0986     xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
0987     if (be->vif) {
0988         kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
0989         backend_disconnect(be);
0990         xenvif_free(be->vif);
0991         be->vif = NULL;
0992     }
0993     kfree(be->hotplug_script);
0994     kfree(be);
0995     dev_set_drvdata(&dev->dev, NULL);
0996     return 0;
0997 }
0998 
0999 /*
1000  * Entry point to this code when a new device is created.  Allocate the basic
1001  * structures and switch to InitWait.
1002  */
1003 static int netback_probe(struct xenbus_device *dev,
1004              const struct xenbus_device_id *id)
1005 {
1006     const char *message;
1007     struct xenbus_transaction xbt;
1008     int err;
1009     int sg;
1010     const char *script;
1011     struct backend_info *be = kzalloc(sizeof(*be), GFP_KERNEL);
1012 
1013     if (!be) {
1014         xenbus_dev_fatal(dev, -ENOMEM,
1015                  "allocating backend structure");
1016         return -ENOMEM;
1017     }
1018 
1019     be->dev = dev;
1020     dev_set_drvdata(&dev->dev, be);
1021 
1022     sg = 1;
1023 
1024     do {
1025         err = xenbus_transaction_start(&xbt);
1026         if (err) {
1027             xenbus_dev_fatal(dev, err, "starting transaction");
1028             goto fail;
1029         }
1030 
1031         err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
1032         if (err) {
1033             message = "writing feature-sg";
1034             goto abort_transaction;
1035         }
1036 
1037         err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
1038                     "%d", sg);
1039         if (err) {
1040             message = "writing feature-gso-tcpv4";
1041             goto abort_transaction;
1042         }
1043 
1044         err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
1045                     "%d", sg);
1046         if (err) {
1047             message = "writing feature-gso-tcpv6";
1048             goto abort_transaction;
1049         }
1050 
1051         /* We support partial checksum setup for IPv6 packets */
1052         err = xenbus_printf(xbt, dev->nodename,
1053                     "feature-ipv6-csum-offload",
1054                     "%d", 1);
1055         if (err) {
1056             message = "writing feature-ipv6-csum-offload";
1057             goto abort_transaction;
1058         }
1059 
1060         /* We support rx-copy path. */
1061         err = xenbus_printf(xbt, dev->nodename,
1062                     "feature-rx-copy", "%d", 1);
1063         if (err) {
1064             message = "writing feature-rx-copy";
1065             goto abort_transaction;
1066         }
1067 
1068         /* we can adjust a headroom for netfront XDP processing */
1069         err = xenbus_printf(xbt, dev->nodename,
1070                     "feature-xdp-headroom", "%d",
1071                     provides_xdp_headroom);
1072         if (err) {
1073             message = "writing feature-xdp-headroom";
1074             goto abort_transaction;
1075         }
1076 
1077         /* We don't support rx-flip path (except old guests who
1078          * don't grok this feature flag).
1079          */
1080         err = xenbus_printf(xbt, dev->nodename,
1081                     "feature-rx-flip", "%d", 0);
1082         if (err) {
1083             message = "writing feature-rx-flip";
1084             goto abort_transaction;
1085         }
1086 
1087         /* We support dynamic multicast-control. */
1088         err = xenbus_printf(xbt, dev->nodename,
1089                     "feature-multicast-control", "%d", 1);
1090         if (err) {
1091             message = "writing feature-multicast-control";
1092             goto abort_transaction;
1093         }
1094 
1095         err = xenbus_printf(xbt, dev->nodename,
1096                     "feature-dynamic-multicast-control",
1097                     "%d", 1);
1098         if (err) {
1099             message = "writing feature-dynamic-multicast-control";
1100             goto abort_transaction;
1101         }
1102 
1103         err = xenbus_transaction_end(xbt, 0);
1104     } while (err == -EAGAIN);
1105 
1106     if (err) {
1107         xenbus_dev_fatal(dev, err, "completing transaction");
1108         goto fail;
1109     }
1110 
1111     /* Split event channels support, this is optional so it is not
1112      * put inside the above loop.
1113      */
1114     err = xenbus_printf(XBT_NIL, dev->nodename,
1115                 "feature-split-event-channels",
1116                 "%u", separate_tx_rx_irq);
1117     if (err)
1118         pr_debug("Error writing feature-split-event-channels\n");
1119 
1120     /* Multi-queue support: This is an optional feature. */
1121     err = xenbus_printf(XBT_NIL, dev->nodename,
1122                 "multi-queue-max-queues", "%u", xenvif_max_queues);
1123     if (err)
1124         pr_debug("Error writing multi-queue-max-queues\n");
1125 
1126     err = xenbus_printf(XBT_NIL, dev->nodename,
1127                 "feature-ctrl-ring",
1128                 "%u", true);
1129     if (err)
1130         pr_debug("Error writing feature-ctrl-ring\n");
1131 
1132     backend_switch_state(be, XenbusStateInitWait);
1133 
1134     script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
1135     if (IS_ERR(script)) {
1136         err = PTR_ERR(script);
1137         xenbus_dev_fatal(dev, err, "reading script");
1138         goto fail;
1139     }
1140 
1141     be->hotplug_script = script;
1142 
1143     /* This kicks hotplug scripts, so do it immediately. */
1144     err = backend_create_xenvif(be);
1145     if (err)
1146         goto fail;
1147 
1148     return 0;
1149 
1150 abort_transaction:
1151     xenbus_transaction_end(xbt, 1);
1152     xenbus_dev_fatal(dev, err, "%s", message);
1153 fail:
1154     pr_debug("failed\n");
1155     netback_remove(dev);
1156     return err;
1157 }
1158 
1159 static const struct xenbus_device_id netback_ids[] = {
1160     { "vif" },
1161     { "" }
1162 };
1163 
1164 static struct xenbus_driver netback_driver = {
1165     .ids = netback_ids,
1166     .probe = netback_probe,
1167     .remove = netback_remove,
1168     .uevent = netback_uevent,
1169     .otherend_changed = frontend_changed,
1170     .allow_rebind = true,
1171 };
1172 
1173 int xenvif_xenbus_init(void)
1174 {
1175     return xenbus_register_backend(&netback_driver);
1176 }
1177 
1178 void xenvif_xenbus_fini(void)
1179 {
1180     return xenbus_unregister_driver(&netback_driver);
1181 }