hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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) {
....@@ -842,6 +872,10 @@
842872 }
843873
844874 pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
875
+ if (pkt_len < 0) {
876
+ vp->in_error = true;
877
+ return pkt_len;
878
+ }
845879
846880 if (skb != NULL) {
847881 if (pkt_len > vp->header_size) {
....@@ -888,11 +922,15 @@
888922
889923 if (iov_count < 1)
890924 goto drop;
925
+
891926 pkt_len = uml_vector_writev(
892927 vp->fds->tx_fd,
893928 (struct iovec *) &iov,
894929 iov_count
895930 );
931
+
932
+ if (pkt_len < 0)
933
+ goto drop;
896934
897935 netif_trans_update(vp->dev);
898936 netif_wake_queue(vp->dev);
....@@ -908,6 +946,8 @@
908946 drop:
909947 vp->dev->stats.tx_dropped++;
910948 consume_skb(skb);
949
+ if (pkt_len < 0)
950
+ vp->in_error = true;
911951 return pkt_len;
912952 }
913953
....@@ -935,6 +975,9 @@
935975
936976 packet_count = uml_vector_recvmmsg(
937977 vp->fds->rx_fd, qi->mmsg_vector, qi->max_depth, 0);
978
+
979
+ if (packet_count < 0)
980
+ vp->in_error = true;
938981
939982 if (packet_count <= 0)
940983 return packet_count;
....@@ -1005,21 +1048,31 @@
10051048 static void vector_rx(struct vector_private *vp)
10061049 {
10071050 int err;
1051
+ int iter = 0;
10081052
10091053 if ((vp->options & VECTOR_RX) > 0)
1010
- while ((err = vector_mmsg_rx(vp)) > 0)
1011
- ;
1054
+ while (((err = vector_mmsg_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1055
+ iter++;
10121056 else
1013
- while ((err = vector_legacy_rx(vp)) > 0)
1014
- ;
1057
+ while (((err = vector_legacy_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1058
+ iter++;
10151059 if ((err != 0) && net_ratelimit())
10161060 netdev_err(vp->dev, "vector_rx: error(%d)\n", err);
1061
+ if (iter == MAX_ITERATIONS)
1062
+ netdev_err(vp->dev, "vector_rx: device stuck, remote end may have closed the connection\n");
10171063 }
10181064
10191065 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
10201066 {
10211067 struct vector_private *vp = netdev_priv(dev);
10221068 int queue_depth = 0;
1069
+
1070
+ if (vp->in_error) {
1071
+ deactivate_fd(vp->fds->rx_fd, vp->rx_irq);
1072
+ if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0))
1073
+ deactivate_fd(vp->fds->tx_fd, vp->tx_irq);
1074
+ return NETDEV_TX_BUSY;
1075
+ }
10231076
10241077 if ((vp->options & VECTOR_TX) == 0) {
10251078 writev_tx(vp, skb);
....@@ -1043,7 +1096,7 @@
10431096 vector_send(vp->tx_queue);
10441097 return NETDEV_TX_OK;
10451098 }
1046
- if (skb->xmit_more) {
1099
+ if (netdev_xmit_more()) {
10471100 mod_timer(&vp->tl, vp->coalesce);
10481101 return NETDEV_TX_OK;
10491102 }
....@@ -1111,6 +1164,8 @@
11111164 }
11121165 tasklet_kill(&vp->tx_poll);
11131166 if (vp->fds->rx_fd > 0) {
1167
+ if (vp->bpf)
1168
+ uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
11141169 os_close_file(vp->fds->rx_fd);
11151170 vp->fds->rx_fd = -1;
11161171 }
....@@ -1119,15 +1174,13 @@
11191174 vp->fds->tx_fd = -1;
11201175 }
11211176 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);
1177
+ kfree(vp->bpf->filter);
1178
+ kfree(vp->bpf);
1179
+ vp->bpf = NULL;
1180
+ kfree(vp->fds->remote_addr);
1181
+ kfree(vp->transport_data);
1182
+ kfree(vp->header_rxbuffer);
1183
+ kfree(vp->header_txbuffer);
11311184 if (vp->rx_queue != NULL)
11321185 destroy_queue(vp->rx_queue);
11331186 if (vp->tx_queue != NULL)
....@@ -1136,6 +1189,7 @@
11361189 vp->fds = NULL;
11371190 spin_lock_irqsave(&vp->lock, flags);
11381191 vp->opened = false;
1192
+ vp->in_error = false;
11391193 spin_unlock_irqrestore(&vp->lock, flags);
11401194 return 0;
11411195 }
....@@ -1157,6 +1211,7 @@
11571211 netif_start_queue(vp->dev);
11581212 netif_wake_queue(vp->dev);
11591213 }
1214
+
11601215 static int vector_net_open(struct net_device *dev)
11611216 {
11621217 struct vector_private *vp = netdev_priv(dev);
....@@ -1171,6 +1226,8 @@
11711226 }
11721227 vp->opened = true;
11731228 spin_unlock_irqrestore(&vp->lock, flags);
1229
+
1230
+ vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed));
11741231
11751232 vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
11761233
....@@ -1243,8 +1300,11 @@
12431300 if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
12441301 vp->options |= VECTOR_BPF;
12451302 }
1246
- if ((vp->options & VECTOR_BPF) != 0)
1247
- vp->bpf = uml_vector_default_bpf(vp->fds->rx_fd, dev->dev_addr);
1303
+ if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL))
1304
+ vp->bpf = uml_vector_default_bpf(dev->dev_addr);
1305
+
1306
+ if (vp->bpf != NULL)
1307
+ uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
12481308
12491309 netif_start_queue(dev);
12501310
....@@ -1274,7 +1334,7 @@
12741334 return;
12751335 }
12761336
1277
-static void vector_net_tx_timeout(struct net_device *dev)
1337
+static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
12781338 {
12791339 struct vector_private *vp = netdev_priv(dev);
12801340
....@@ -1320,7 +1380,65 @@
13201380 struct ethtool_drvinfo *info)
13211381 {
13221382 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1323
- strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
1383
+}
1384
+
1385
+static int vector_net_load_bpf_flash(struct net_device *dev,
1386
+ struct ethtool_flash *efl)
1387
+{
1388
+ struct vector_private *vp = netdev_priv(dev);
1389
+ struct vector_device *vdevice;
1390
+ const struct firmware *fw;
1391
+ int result = 0;
1392
+
1393
+ if (!(vp->options & VECTOR_BPF_FLASH)) {
1394
+ netdev_err(dev, "loading firmware not permitted: %s\n", efl->data);
1395
+ return -1;
1396
+ }
1397
+
1398
+ spin_lock(&vp->lock);
1399
+
1400
+ if (vp->bpf != NULL) {
1401
+ if (vp->opened)
1402
+ uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1403
+ kfree(vp->bpf->filter);
1404
+ vp->bpf->filter = NULL;
1405
+ } else {
1406
+ vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC);
1407
+ if (vp->bpf == NULL) {
1408
+ netdev_err(dev, "failed to allocate memory for firmware\n");
1409
+ goto flash_fail;
1410
+ }
1411
+ }
1412
+
1413
+ vdevice = find_device(vp->unit);
1414
+
1415
+ if (request_firmware(&fw, efl->data, &vdevice->pdev.dev))
1416
+ goto flash_fail;
1417
+
1418
+ vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC);
1419
+ if (!vp->bpf->filter)
1420
+ goto free_buffer;
1421
+
1422
+ vp->bpf->len = fw->size / sizeof(struct sock_filter);
1423
+ release_firmware(fw);
1424
+
1425
+ if (vp->opened)
1426
+ result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1427
+
1428
+ spin_unlock(&vp->lock);
1429
+
1430
+ return result;
1431
+
1432
+free_buffer:
1433
+ release_firmware(fw);
1434
+
1435
+flash_fail:
1436
+ spin_unlock(&vp->lock);
1437
+ if (vp->bpf != NULL)
1438
+ kfree(vp->bpf->filter);
1439
+ kfree(vp->bpf);
1440
+ vp->bpf = NULL;
1441
+ return -1;
13241442 }
13251443
13261444 static void vector_get_ringparam(struct net_device *netdev,
....@@ -1391,6 +1509,7 @@
13911509 }
13921510
13931511 static const struct ethtool_ops vector_net_ethtool_ops = {
1512
+ .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS,
13941513 .get_drvinfo = vector_net_get_drvinfo,
13951514 .get_link = ethtool_op_get_link,
13961515 .get_ts_info = ethtool_op_get_ts_info,
....@@ -1400,6 +1519,7 @@
14001519 .get_ethtool_stats = vector_get_ethtool_stats,
14011520 .get_coalesce = vector_get_coalesce,
14021521 .set_coalesce = vector_set_coalesce,
1522
+ .flash_device = vector_net_load_bpf_flash,
14031523 };
14041524
14051525
....@@ -1503,8 +1623,10 @@
15031623 .transport_data = NULL,
15041624 .in_write_poll = false,
15051625 .coalesce = 2,
1506
- .req_size = get_req_size(def)
1507
- });
1626
+ .req_size = get_req_size(def),
1627
+ .in_error = false,
1628
+ .bpf = NULL
1629
+ });
15081630
15091631 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
15101632 tasklet_init(&vp->tx_poll, vector_tx_poll, (unsigned long)vp);
....@@ -1580,7 +1702,10 @@
15801702 str, error);
15811703 return 1;
15821704 }
1583
- new = alloc_bootmem(sizeof(*new));
1705
+ new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
1706
+ if (!new)
1707
+ panic("%s: Failed to allocate %zu bytes\n", __func__,
1708
+ sizeof(*new));
15841709 INIT_LIST_HEAD(&new->list);
15851710 new->unit = n;
15861711 new->arguments = str;