hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/arch/um/drivers/vector_kern.c
....@@ -1,15 +1,15 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
2
- * Copyright (C) 2017 - Cambridge Greys Limited
3
+ * Copyright (C) 2017 - 2019 Cambridge Greys Limited
34 * Copyright (C) 2011 - 2014 Cisco Systems Inc
45 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
56 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
67 * James Leu (jleu@mindspring.net).
78 * Copyright (C) 2001 by various other people who didn't put their name here.
8
- * Licensed under the GPL.
99 */
1010
1111 #include <linux/version.h>
12
-#include <linux/bootmem.h>
12
+#include <linux/memblock.h>
1313 #include <linux/etherdevice.h>
1414 #include <linux/ethtool.h>
1515 #include <linux/inetdevice.h>
....@@ -21,6 +21,9 @@
2121 #include <linux/skbuff.h>
2222 #include <linux/slab.h>
2323 #include <linux/interrupt.h>
24
+#include <linux/firmware.h>
25
+#include <linux/fs.h>
26
+#include <uapi/linux/filter.h>
2427 #include <init.h>
2528 #include <irq_kern.h>
2629 #include <irq_user.h>
....@@ -43,7 +46,6 @@
4346
4447
4548 #define DRIVER_NAME "uml-vector"
46
-#define DRIVER_VERSION "01"
4749 struct vector_cmd_line_arg {
4850 struct list_head list;
4951 int unit;
....@@ -76,6 +78,7 @@
7678 #define DEFAULT_VECTOR_SIZE 64
7779 #define TX_SMALL_PACKET 128
7880 #define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1)
81
+#define MAX_ITERATIONS 64
7982
8083 static const struct {
8184 const char string[ETH_GSTRING_LEN];
....@@ -121,9 +124,27 @@
121124
122125 if (mtu != NULL) {
123126 if (kstrtoul(mtu, 10, &result) == 0)
124
- return result;
127
+ if ((result < (1 << 16) - 1) && (result >= 576))
128
+ return result;
125129 }
126130 return ETH_MAX_PACKET;
131
+}
132
+
133
+static char *get_bpf_file(struct arglist *def)
134
+{
135
+ return uml_vector_fetch_arg(def, "bpffile");
136
+}
137
+
138
+static bool get_bpf_flash(struct arglist *def)
139
+{
140
+ char *allow = uml_vector_fetch_arg(def, "bpfflash");
141
+ long result;
142
+
143
+ if (allow != NULL) {
144
+ if (kstrtoul(allow, 10, &result) == 0)
145
+ return (allow > 0);
146
+ }
147
+ return false;
127148 }
128149
129150 static int get_depth(struct arglist *def)
....@@ -174,6 +195,10 @@
174195 int vec_rx = VECTOR_RX;
175196 int vec_tx = VECTOR_TX;
176197 long parsed;
198
+ int result = 0;
199
+
200
+ if (transport == NULL)
201
+ return -EINVAL;
177202
178203 if (vector != NULL) {
179204 if (kstrtoul(vector, 10, &parsed) == 0) {
....@@ -184,12 +209,16 @@
184209 }
185210 }
186211
212
+ if (get_bpf_flash(def))
213
+ result = VECTOR_BPF_FLASH;
187214
188215 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
189
- return (vec_rx | VECTOR_BPF);
216
+ return result;
217
+ if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
218
+ return (result | vec_rx | VECTOR_BPF);
190219 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
191
- return (vec_rx | vec_tx | VECTOR_QDISC_BYPASS);
192
- return (vec_rx | vec_tx);
220
+ return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS);
221
+ return (result | vec_rx | vec_tx);
193222 }
194223
195224
....@@ -415,6 +444,7 @@
415444 if (net_ratelimit())
416445 netdev_err(vp->dev, "sendmmsg err=%i\n",
417446 result);
447
+ vp->in_error = true;
418448 result = send_len;
419449 }
420450 if (result > 0) {
....@@ -741,6 +771,7 @@
741771
742772 if (parsed == NULL) {
743773 *error_out = "vector_config failed to parse parameters";
774
+ kfree(params);
744775 return -EINVAL;
745776 }
746777
....@@ -842,6 +873,10 @@
842873 }
843874
844875 pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
876
+ if (pkt_len < 0) {
877
+ vp->in_error = true;
878
+ return pkt_len;
879
+ }
845880
846881 if (skb != NULL) {
847882 if (pkt_len > vp->header_size) {
....@@ -888,11 +923,15 @@
888923
889924 if (iov_count < 1)
890925 goto drop;
926
+
891927 pkt_len = uml_vector_writev(
892928 vp->fds->tx_fd,
893929 (struct iovec *) &iov,
894930 iov_count
895931 );
932
+
933
+ if (pkt_len < 0)
934
+ goto drop;
896935
897936 netif_trans_update(vp->dev);
898937 netif_wake_queue(vp->dev);
....@@ -908,6 +947,8 @@
908947 drop:
909948 vp->dev->stats.tx_dropped++;
910949 consume_skb(skb);
950
+ if (pkt_len < 0)
951
+ vp->in_error = true;
911952 return pkt_len;
912953 }
913954
....@@ -935,6 +976,9 @@
935976
936977 packet_count = uml_vector_recvmmsg(
937978 vp->fds->rx_fd, qi->mmsg_vector, qi->max_depth, 0);
979
+
980
+ if (packet_count < 0)
981
+ vp->in_error = true;
938982
939983 if (packet_count <= 0)
940984 return packet_count;
....@@ -1005,21 +1049,31 @@
10051049 static void vector_rx(struct vector_private *vp)
10061050 {
10071051 int err;
1052
+ int iter = 0;
10081053
10091054 if ((vp->options & VECTOR_RX) > 0)
1010
- while ((err = vector_mmsg_rx(vp)) > 0)
1011
- ;
1055
+ while (((err = vector_mmsg_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1056
+ iter++;
10121057 else
1013
- while ((err = vector_legacy_rx(vp)) > 0)
1014
- ;
1058
+ while (((err = vector_legacy_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1059
+ iter++;
10151060 if ((err != 0) && net_ratelimit())
10161061 netdev_err(vp->dev, "vector_rx: error(%d)\n", err);
1062
+ if (iter == MAX_ITERATIONS)
1063
+ netdev_err(vp->dev, "vector_rx: device stuck, remote end may have closed the connection\n");
10171064 }
10181065
10191066 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
10201067 {
10211068 struct vector_private *vp = netdev_priv(dev);
10221069 int queue_depth = 0;
1070
+
1071
+ if (vp->in_error) {
1072
+ deactivate_fd(vp->fds->rx_fd, vp->rx_irq);
1073
+ if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0))
1074
+ deactivate_fd(vp->fds->tx_fd, vp->tx_irq);
1075
+ return NETDEV_TX_BUSY;
1076
+ }
10231077
10241078 if ((vp->options & VECTOR_TX) == 0) {
10251079 writev_tx(vp, skb);
....@@ -1043,7 +1097,7 @@
10431097 vector_send(vp->tx_queue);
10441098 return NETDEV_TX_OK;
10451099 }
1046
- if (skb->xmit_more) {
1100
+ if (netdev_xmit_more()) {
10471101 mod_timer(&vp->tl, vp->coalesce);
10481102 return NETDEV_TX_OK;
10491103 }
....@@ -1111,6 +1165,8 @@
11111165 }
11121166 tasklet_kill(&vp->tx_poll);
11131167 if (vp->fds->rx_fd > 0) {
1168
+ if (vp->bpf)
1169
+ uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
11141170 os_close_file(vp->fds->rx_fd);
11151171 vp->fds->rx_fd = -1;
11161172 }
....@@ -1119,15 +1175,13 @@
11191175 vp->fds->tx_fd = -1;
11201176 }
11211177 if (vp->bpf != NULL)
1122
- kfree(vp->bpf);
1123
- if (vp->fds->remote_addr != NULL)
1124
- kfree(vp->fds->remote_addr);
1125
- if (vp->transport_data != NULL)
1126
- kfree(vp->transport_data);
1127
- if (vp->header_rxbuffer != NULL)
1128
- kfree(vp->header_rxbuffer);
1129
- if (vp->header_txbuffer != NULL)
1130
- kfree(vp->header_txbuffer);
1178
+ kfree(vp->bpf->filter);
1179
+ kfree(vp->bpf);
1180
+ vp->bpf = NULL;
1181
+ kfree(vp->fds->remote_addr);
1182
+ kfree(vp->transport_data);
1183
+ kfree(vp->header_rxbuffer);
1184
+ kfree(vp->header_txbuffer);
11311185 if (vp->rx_queue != NULL)
11321186 destroy_queue(vp->rx_queue);
11331187 if (vp->tx_queue != NULL)
....@@ -1136,6 +1190,7 @@
11361190 vp->fds = NULL;
11371191 spin_lock_irqsave(&vp->lock, flags);
11381192 vp->opened = false;
1193
+ vp->in_error = false;
11391194 spin_unlock_irqrestore(&vp->lock, flags);
11401195 return 0;
11411196 }
....@@ -1157,6 +1212,7 @@
11571212 netif_start_queue(vp->dev);
11581213 netif_wake_queue(vp->dev);
11591214 }
1215
+
11601216 static int vector_net_open(struct net_device *dev)
11611217 {
11621218 struct vector_private *vp = netdev_priv(dev);
....@@ -1171,6 +1227,8 @@
11711227 }
11721228 vp->opened = true;
11731229 spin_unlock_irqrestore(&vp->lock, flags);
1230
+
1231
+ vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed));
11741232
11751233 vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
11761234
....@@ -1243,8 +1301,11 @@
12431301 if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
12441302 vp->options |= VECTOR_BPF;
12451303 }
1246
- if ((vp->options & VECTOR_BPF) != 0)
1247
- vp->bpf = uml_vector_default_bpf(vp->fds->rx_fd, dev->dev_addr);
1304
+ if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL))
1305
+ vp->bpf = uml_vector_default_bpf(dev->dev_addr);
1306
+
1307
+ if (vp->bpf != NULL)
1308
+ uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
12481309
12491310 netif_start_queue(dev);
12501311
....@@ -1274,7 +1335,7 @@
12741335 return;
12751336 }
12761337
1277
-static void vector_net_tx_timeout(struct net_device *dev)
1338
+static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
12781339 {
12791340 struct vector_private *vp = netdev_priv(dev);
12801341
....@@ -1320,7 +1381,65 @@
13201381 struct ethtool_drvinfo *info)
13211382 {
13221383 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1323
- strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
1384
+}
1385
+
1386
+static int vector_net_load_bpf_flash(struct net_device *dev,
1387
+ struct ethtool_flash *efl)
1388
+{
1389
+ struct vector_private *vp = netdev_priv(dev);
1390
+ struct vector_device *vdevice;
1391
+ const struct firmware *fw;
1392
+ int result = 0;
1393
+
1394
+ if (!(vp->options & VECTOR_BPF_FLASH)) {
1395
+ netdev_err(dev, "loading firmware not permitted: %s\n", efl->data);
1396
+ return -1;
1397
+ }
1398
+
1399
+ spin_lock(&vp->lock);
1400
+
1401
+ if (vp->bpf != NULL) {
1402
+ if (vp->opened)
1403
+ uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1404
+ kfree(vp->bpf->filter);
1405
+ vp->bpf->filter = NULL;
1406
+ } else {
1407
+ vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC);
1408
+ if (vp->bpf == NULL) {
1409
+ netdev_err(dev, "failed to allocate memory for firmware\n");
1410
+ goto flash_fail;
1411
+ }
1412
+ }
1413
+
1414
+ vdevice = find_device(vp->unit);
1415
+
1416
+ if (request_firmware(&fw, efl->data, &vdevice->pdev.dev))
1417
+ goto flash_fail;
1418
+
1419
+ vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC);
1420
+ if (!vp->bpf->filter)
1421
+ goto free_buffer;
1422
+
1423
+ vp->bpf->len = fw->size / sizeof(struct sock_filter);
1424
+ release_firmware(fw);
1425
+
1426
+ if (vp->opened)
1427
+ result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1428
+
1429
+ spin_unlock(&vp->lock);
1430
+
1431
+ return result;
1432
+
1433
+free_buffer:
1434
+ release_firmware(fw);
1435
+
1436
+flash_fail:
1437
+ spin_unlock(&vp->lock);
1438
+ if (vp->bpf != NULL)
1439
+ kfree(vp->bpf->filter);
1440
+ kfree(vp->bpf);
1441
+ vp->bpf = NULL;
1442
+ return -1;
13241443 }
13251444
13261445 static void vector_get_ringparam(struct net_device *netdev,
....@@ -1391,6 +1510,7 @@
13911510 }
13921511
13931512 static const struct ethtool_ops vector_net_ethtool_ops = {
1513
+ .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS,
13941514 .get_drvinfo = vector_net_get_drvinfo,
13951515 .get_link = ethtool_op_get_link,
13961516 .get_ts_info = ethtool_op_get_ts_info,
....@@ -1400,6 +1520,7 @@
14001520 .get_ethtool_stats = vector_get_ethtool_stats,
14011521 .get_coalesce = vector_get_coalesce,
14021522 .set_coalesce = vector_set_coalesce,
1523
+ .flash_device = vector_net_load_bpf_flash,
14031524 };
14041525
14051526
....@@ -1503,8 +1624,10 @@
15031624 .transport_data = NULL,
15041625 .in_write_poll = false,
15051626 .coalesce = 2,
1506
- .req_size = get_req_size(def)
1507
- });
1627
+ .req_size = get_req_size(def),
1628
+ .in_error = false,
1629
+ .bpf = NULL
1630
+ });
15081631
15091632 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
15101633 tasklet_init(&vp->tx_poll, vector_tx_poll, (unsigned long)vp);
....@@ -1580,7 +1703,10 @@
15801703 str, error);
15811704 return 1;
15821705 }
1583
- new = alloc_bootmem(sizeof(*new));
1706
+ new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
1707
+ if (!new)
1708
+ panic("%s: Failed to allocate %zu bytes\n", __func__,
1709
+ sizeof(*new));
15841710 INIT_LIST_HEAD(&new->list);
15851711 new->unit = n;
15861712 new->arguments = str;