usb: gadget: ncm: Add support to update wMaxSegmentSize via configfs
authorKrishna Kurapati <quic_kriskura@quicinc.com>
Thu, 21 Dec 2023 15:32:16 +0000 (21:02 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 4 Jan 2024 15:01:14 +0000 (16:01 +0100)
The max segment size is currently limited to the ethernet frame length of
the kernel which happens to be 1514 at this point in time. However the NCM
specification limits it to 64K for sixtenn bit NTB's. For peer to peer
connections, increasing the segment size gives better throughput.

Add support to configure this value before configfs symlink is created.
Also since the NTB Out/In buffer sizes are fixed at 16384 bytes, limit the
segment size to an upper cap of 8000 to allow at least a minimum of 2 MTU
sized datagrams to be aggregated.

Set the default MTU size for the ncm interface during function bind before
network interface is registered allowing MTU to be set in parity with
wMaxSegmentSize.

Update gadget documentation describing the new configfs property.

Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
Link: https://lore.kernel.org/r/20231221153216.18657-1-quic_kriskura@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/usb/gadget-testing.rst
drivers/usb/gadget/function/f_ncm.c
drivers/usb/gadget/function/u_ncm.h

index 29072c166d236a25e175df3f1677b607a4449013..8cd62c466d20aac597fa5aa15ecfb2930c15c252 100644 (file)
@@ -448,15 +448,17 @@ Function-specific configfs interface
 The function name to use when creating the function directory is "ncm".
 The NCM function provides these attributes in its function directory:
 
-       =============== ==================================================
-       ifname          network device interface name associated with this
-                       function instance
-       qmult           queue length multiplier for high and super speed
-       host_addr       MAC address of host's end of this
-                       Ethernet over USB link
-       dev_addr        MAC address of device's end of this
-                       Ethernet over USB link
-       =============== ==================================================
+       ===============   ==================================================
+       ifname            network device interface name associated with this
+                         function instance
+       qmult             queue length multiplier for high and super speed
+       host_addr         MAC address of host's end of this
+                         Ethernet over USB link
+       dev_addr          MAC address of device's end of this
+                         Ethernet over USB link
+       max_segment_size  Segment size required for P2P connections. This
+                         will set MTU to (max_segment_size - 14 bytes)
+       ===============   ==================================================
 
 and after creating the functions/ncm.<instance name> they contain default
 values: qmult is 5, dev_addr and host_addr are randomly selected.
index cc0ed29a4adc07b62fa412802c2b42495e5c22d8..a1575a0ca568d7c46bc23bd605404096db14e6e5 100644 (file)
@@ -103,6 +103,16 @@ static inline struct f_ncm *func_to_ncm(struct usb_function *f)
 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
 #define TX_TIMEOUT_NSECS       300000
 
+/*
+ * Although max mtu as dictated by u_ether is 15412 bytes, setting
+ * max_segment_sizeto 15426 would not be efficient. If user chooses segment
+ * size to be (>= 8192), then we can't aggregate more than one  buffer in each
+ * NTB (assuming each packet coming from network layer is >= 8192 bytes) as ep
+ * maxpacket limit is 16384. So let max_segment_size be limited to 8000 to allow
+ * at least 2 packets to be aggregated reducing wastage of NTB buffer space
+ */
+#define MAX_DATAGRAM_SIZE      8000
+
 #define FORMATS_SUPPORTED      (USB_CDC_NCM_NTB16_SUPPORTED |  \
                                 USB_CDC_NCM_NTB32_SUPPORTED)
 
@@ -179,7 +189,6 @@ static struct usb_cdc_ether_desc ecm_desc = {
        /* this descriptor actually adds value, surprise! */
        /* .iMACAddress = DYNAMIC */
        .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
-       .wMaxSegmentSize =      cpu_to_le16(ETH_FRAME_LEN),
        .wNumberMCFilters =     cpu_to_le16(0),
        .bNumberPowerFilters =  0,
 };
@@ -1166,11 +1175,15 @@ static int ncm_unwrap_ntb(struct gether *port,
        struct sk_buff  *skb2;
        int             ret = -EINVAL;
        unsigned        ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
-       unsigned        frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
+       unsigned        frame_max;
        const struct ndp_parser_opts *opts = ncm->parser_opts;
        unsigned        crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
        int             dgram_counter;
        int             to_process = skb->len;
+       struct f_ncm_opts *ncm_opts;
+
+       ncm_opts = container_of(port->func.fi, struct f_ncm_opts, func_inst);
+       frame_max = ncm_opts->max_segment_size;
 
 parse_ntb:
        tmp = (__le16 *)ntb_ptr;
@@ -1430,8 +1443,10 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
 
        mutex_lock(&ncm_opts->lock);
        gether_set_gadget(ncm_opts->net, cdev->gadget);
-       if (!ncm_opts->bound)
+       if (!ncm_opts->bound) {
+               ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN);
                status = gether_register_netdev(ncm_opts->net);
+       }
        mutex_unlock(&ncm_opts->lock);
 
        if (status)
@@ -1474,6 +1489,8 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
        ncm_data_intf.bInterfaceNumber = status;
        ncm_union_desc.bSlaveInterface0 = status;
 
+       ecm_desc.wMaxSegmentSize = ncm_opts->max_segment_size;
+
        status = -ENODEV;
 
        /* allocate instance-specific endpoints */
@@ -1576,11 +1593,56 @@ USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
 /* f_ncm_opts_ifname */
 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
 
+static ssize_t ncm_opts_max_segment_size_show(struct config_item *item,
+                                             char *page)
+{
+       struct f_ncm_opts *opts = to_f_ncm_opts(item);
+       u16 segment_size;
+
+       mutex_lock(&opts->lock);
+       segment_size = opts->max_segment_size;
+       mutex_unlock(&opts->lock);
+
+       return sysfs_emit(page, "%u\n", segment_size);
+}
+
+static ssize_t ncm_opts_max_segment_size_store(struct config_item *item,
+                                              const char *page, size_t len)
+{
+       struct f_ncm_opts *opts = to_f_ncm_opts(item);
+       u16 segment_size;
+       int ret;
+
+       mutex_lock(&opts->lock);
+       if (opts->refcnt) {
+               ret = -EBUSY;
+               goto out;
+       }
+
+       ret = kstrtou16(page, 0, &segment_size);
+       if (ret)
+               goto out;
+
+       if (segment_size > MAX_DATAGRAM_SIZE) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       opts->max_segment_size = segment_size;
+       ret = len;
+out:
+       mutex_unlock(&opts->lock);
+       return ret;
+}
+
+CONFIGFS_ATTR(ncm_opts_, max_segment_size);
+
 static struct configfs_attribute *ncm_attrs[] = {
        &ncm_opts_attr_dev_addr,
        &ncm_opts_attr_host_addr,
        &ncm_opts_attr_qmult,
        &ncm_opts_attr_ifname,
+       &ncm_opts_attr_max_segment_size,
        NULL,
 };
 
@@ -1623,6 +1685,7 @@ static struct usb_function_instance *ncm_alloc_inst(void)
                kfree(opts);
                return ERR_CAST(net);
        }
+       opts->max_segment_size = cpu_to_le16(ETH_FRAME_LEN);
        INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
 
        descs[0] = &opts->ncm_os_desc;
index 5408854d84072dc7e35646ee978fb09cdeaa7f19..49ec095cdb4b6dcb330fd3149b502840312f78ec 100644 (file)
@@ -31,6 +31,8 @@ struct f_ncm_opts {
         */
        struct mutex                    lock;
        int                             refcnt;
+
+       u16                             max_segment_size;
 };
 
 #endif /* U_NCM_H */