hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/net/xen-netback/xenbus.c
....@@ -1,42 +1,14 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Xenbus code for netif backend
34 *
45 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
56 * Copyright (C) 2005 XenSource Ltd
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
197 */
208
219 #include "common.h"
2210 #include <linux/vmalloc.h>
2311 #include <linux/rtnetlink.h>
24
-
25
-struct backend_info {
26
- struct xenbus_device *dev;
27
- struct xenvif *vif;
28
-
29
- /* This is the state that will be reflected in xenstore when any
30
- * active hotplug script completes.
31
- */
32
- enum xenbus_state state;
33
-
34
- enum xenbus_state frontend_state;
35
- struct xenbus_watch hotplug_status_watch;
36
- u8 have_hotplug_status_watch:1;
37
-
38
- const char *hotplug_script;
39
-};
4012
4113 static int connect_data_rings(struct backend_info *be,
4214 struct xenvif_queue *queue);
....@@ -186,7 +158,7 @@
186158 .write = xenvif_write_io_ring,
187159 };
188160
189
-static int xenvif_read_ctrl(struct seq_file *m, void *v)
161
+static int xenvif_ctrl_show(struct seq_file *m, void *v)
190162 {
191163 struct xenvif *vif = m->private;
192164
....@@ -194,250 +166,34 @@
194166
195167 return 0;
196168 }
197
-
198
-static int xenvif_ctrl_open(struct inode *inode, struct file *filp)
199
-{
200
- return single_open(filp, xenvif_read_ctrl, inode->i_private);
201
-}
202
-
203
-static const struct file_operations xenvif_dbg_ctrl_ops_fops = {
204
- .owner = THIS_MODULE,
205
- .open = xenvif_ctrl_open,
206
- .read = seq_read,
207
- .llseek = seq_lseek,
208
- .release = single_release,
209
-};
169
+DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
210170
211171 static void xenvif_debugfs_addif(struct xenvif *vif)
212172 {
213
- struct dentry *pfile;
214173 int i;
215
-
216
- if (IS_ERR_OR_NULL(xen_netback_dbg_root))
217
- return;
218174
219175 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
220176 xen_netback_dbg_root);
221
- if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
222
- for (i = 0; i < vif->num_queues; ++i) {
223
- char filename[sizeof("io_ring_q") + 4];
177
+ for (i = 0; i < vif->num_queues; ++i) {
178
+ char filename[sizeof("io_ring_q") + 4];
224179
225
- snprintf(filename, sizeof(filename), "io_ring_q%d", i);
226
- pfile = debugfs_create_file(filename,
227
- 0600,
228
- vif->xenvif_dbg_root,
229
- &vif->queues[i],
230
- &xenvif_dbg_io_ring_ops_fops);
231
- if (IS_ERR_OR_NULL(pfile))
232
- pr_warn("Creation of io_ring file returned %ld!\n",
233
- PTR_ERR(pfile));
234
- }
180
+ snprintf(filename, sizeof(filename), "io_ring_q%d", i);
181
+ debugfs_create_file(filename, 0600, vif->xenvif_dbg_root,
182
+ &vif->queues[i],
183
+ &xenvif_dbg_io_ring_ops_fops);
184
+ }
235185
236
- if (vif->ctrl_irq) {
237
- pfile = debugfs_create_file("ctrl",
238
- 0400,
239
- vif->xenvif_dbg_root,
240
- vif,
241
- &xenvif_dbg_ctrl_ops_fops);
242
- if (IS_ERR_OR_NULL(pfile))
243
- pr_warn("Creation of ctrl file returned %ld!\n",
244
- PTR_ERR(pfile));
245
- }
246
- } else
247
- netdev_warn(vif->dev,
248
- "Creation of vif debugfs dir returned %ld!\n",
249
- PTR_ERR(vif->xenvif_dbg_root));
186
+ if (vif->ctrl_irq)
187
+ debugfs_create_file("ctrl", 0400, vif->xenvif_dbg_root, vif,
188
+ &xenvif_ctrl_fops);
250189 }
251190
252191 static void xenvif_debugfs_delif(struct xenvif *vif)
253192 {
254
- if (IS_ERR_OR_NULL(xen_netback_dbg_root))
255
- return;
256
-
257
- if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
258
- debugfs_remove_recursive(vif->xenvif_dbg_root);
193
+ debugfs_remove_recursive(vif->xenvif_dbg_root);
259194 vif->xenvif_dbg_root = NULL;
260195 }
261196 #endif /* CONFIG_DEBUG_FS */
262
-
263
-static int netback_remove(struct xenbus_device *dev)
264
-{
265
- struct backend_info *be = dev_get_drvdata(&dev->dev);
266
-
267
- set_backend_state(be, XenbusStateClosed);
268
-
269
- unregister_hotplug_status_watch(be);
270
- if (be->vif) {
271
- kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
272
- xen_unregister_watchers(be->vif);
273
- xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
274
- xenvif_free(be->vif);
275
- be->vif = NULL;
276
- }
277
- kfree(be->hotplug_script);
278
- kfree(be);
279
- dev_set_drvdata(&dev->dev, NULL);
280
- return 0;
281
-}
282
-
283
-
284
-/**
285
- * Entry point to this code when a new device is created. Allocate the basic
286
- * structures and switch to InitWait.
287
- */
288
-static int netback_probe(struct xenbus_device *dev,
289
- const struct xenbus_device_id *id)
290
-{
291
- const char *message;
292
- struct xenbus_transaction xbt;
293
- int err;
294
- int sg;
295
- const char *script;
296
- struct backend_info *be = kzalloc(sizeof(struct backend_info),
297
- GFP_KERNEL);
298
- if (!be) {
299
- xenbus_dev_fatal(dev, -ENOMEM,
300
- "allocating backend structure");
301
- return -ENOMEM;
302
- }
303
-
304
- be->dev = dev;
305
- dev_set_drvdata(&dev->dev, be);
306
-
307
- be->state = XenbusStateInitialising;
308
- err = xenbus_switch_state(dev, XenbusStateInitialising);
309
- if (err)
310
- goto fail;
311
-
312
- sg = 1;
313
-
314
- do {
315
- err = xenbus_transaction_start(&xbt);
316
- if (err) {
317
- xenbus_dev_fatal(dev, err, "starting transaction");
318
- goto fail;
319
- }
320
-
321
- err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
322
- if (err) {
323
- message = "writing feature-sg";
324
- goto abort_transaction;
325
- }
326
-
327
- err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
328
- "%d", sg);
329
- if (err) {
330
- message = "writing feature-gso-tcpv4";
331
- goto abort_transaction;
332
- }
333
-
334
- err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
335
- "%d", sg);
336
- if (err) {
337
- message = "writing feature-gso-tcpv6";
338
- goto abort_transaction;
339
- }
340
-
341
- /* We support partial checksum setup for IPv6 packets */
342
- err = xenbus_printf(xbt, dev->nodename,
343
- "feature-ipv6-csum-offload",
344
- "%d", 1);
345
- if (err) {
346
- message = "writing feature-ipv6-csum-offload";
347
- goto abort_transaction;
348
- }
349
-
350
- /* We support rx-copy path. */
351
- err = xenbus_printf(xbt, dev->nodename,
352
- "feature-rx-copy", "%d", 1);
353
- if (err) {
354
- message = "writing feature-rx-copy";
355
- goto abort_transaction;
356
- }
357
-
358
- /*
359
- * We don't support rx-flip path (except old guests who don't
360
- * grok this feature flag).
361
- */
362
- err = xenbus_printf(xbt, dev->nodename,
363
- "feature-rx-flip", "%d", 0);
364
- if (err) {
365
- message = "writing feature-rx-flip";
366
- goto abort_transaction;
367
- }
368
-
369
- /* We support dynamic multicast-control. */
370
- err = xenbus_printf(xbt, dev->nodename,
371
- "feature-multicast-control", "%d", 1);
372
- if (err) {
373
- message = "writing feature-multicast-control";
374
- goto abort_transaction;
375
- }
376
-
377
- err = xenbus_printf(xbt, dev->nodename,
378
- "feature-dynamic-multicast-control",
379
- "%d", 1);
380
- if (err) {
381
- message = "writing feature-dynamic-multicast-control";
382
- goto abort_transaction;
383
- }
384
-
385
- err = xenbus_transaction_end(xbt, 0);
386
- } while (err == -EAGAIN);
387
-
388
- if (err) {
389
- xenbus_dev_fatal(dev, err, "completing transaction");
390
- goto fail;
391
- }
392
-
393
- /*
394
- * Split event channels support, this is optional so it is not
395
- * put inside the above loop.
396
- */
397
- err = xenbus_printf(XBT_NIL, dev->nodename,
398
- "feature-split-event-channels",
399
- "%u", separate_tx_rx_irq);
400
- if (err)
401
- pr_debug("Error writing feature-split-event-channels\n");
402
-
403
- /* Multi-queue support: This is an optional feature. */
404
- err = xenbus_printf(XBT_NIL, dev->nodename,
405
- "multi-queue-max-queues", "%u", xenvif_max_queues);
406
- if (err)
407
- pr_debug("Error writing multi-queue-max-queues\n");
408
-
409
- err = xenbus_printf(XBT_NIL, dev->nodename,
410
- "feature-ctrl-ring",
411
- "%u", true);
412
- if (err)
413
- pr_debug("Error writing feature-ctrl-ring\n");
414
-
415
- script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
416
- if (IS_ERR(script)) {
417
- err = PTR_ERR(script);
418
- xenbus_dev_fatal(dev, err, "reading script");
419
- goto fail;
420
- }
421
-
422
- be->hotplug_script = script;
423
-
424
-
425
- /* This kicks hotplug scripts, so do it immediately. */
426
- err = backend_create_xenvif(be);
427
- if (err)
428
- goto fail;
429
-
430
- return 0;
431
-
432
-abort_transaction:
433
- xenbus_transaction_end(xbt, 1);
434
- xenbus_dev_fatal(dev, err, "%s", message);
435
-fail:
436
- pr_debug("failed\n");
437
- netback_remove(dev);
438
- return err;
439
-}
440
-
441197
442198 /*
443199 * Handle the creation of the hotplug script environment. We add the script
....@@ -485,6 +241,7 @@
485241 return err;
486242 }
487243 be->vif = vif;
244
+ vif->be = be;
488245
489246 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
490247 return 0;
....@@ -636,6 +393,24 @@
636393 }
637394 }
638395
396
+static void read_xenbus_frontend_xdp(struct backend_info *be,
397
+ struct xenbus_device *dev)
398
+{
399
+ struct xenvif *vif = be->vif;
400
+ u16 headroom;
401
+ int err;
402
+
403
+ err = xenbus_scanf(XBT_NIL, dev->otherend,
404
+ "xdp-headroom", "%hu", &headroom);
405
+ if (err != 1) {
406
+ vif->xdp_headroom = 0;
407
+ return;
408
+ }
409
+ if (headroom > XEN_NETIF_MAX_XDP_HEADROOM)
410
+ headroom = XEN_NETIF_MAX_XDP_HEADROOM;
411
+ vif->xdp_headroom = headroom;
412
+}
413
+
639414 /**
640415 * Callback received when the frontend's state changes.
641416 */
....@@ -660,6 +435,11 @@
660435 set_backend_state(be, XenbusStateConnected);
661436 break;
662437
438
+ case XenbusStateReconfiguring:
439
+ read_xenbus_frontend_xdp(be, dev);
440
+ xenbus_switch_state(dev, XenbusStateReconfigured);
441
+ break;
442
+
663443 case XenbusStateClosing:
664444 set_backend_state(be, XenbusStateClosing);
665445 break;
....@@ -668,7 +448,7 @@
668448 set_backend_state(be, XenbusStateClosed);
669449 if (xenbus_dev_is_online(dev))
670450 break;
671
- /* fall through if not online */
451
+ fallthrough; /* if not online */
672452 case XenbusStateUnknown:
673453 set_backend_state(be, XenbusStateClosed);
674454 device_unregister(&dev->dev);
....@@ -1043,15 +823,11 @@
1043823 xenvif_carrier_on(be->vif);
1044824
1045825 unregister_hotplug_status_watch(be);
1046
- if (xenbus_exists(XBT_NIL, dev->nodename, "hotplug-status")) {
1047
- err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
1048
- NULL, hotplug_status_changed,
1049
- "%s/%s", dev->nodename,
1050
- "hotplug-status");
1051
- if (err)
1052
- goto err;
826
+ err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, NULL,
827
+ hotplug_status_changed,
828
+ "%s/%s", dev->nodename, "hotplug-status");
829
+ if (!err)
1053830 be->have_hotplug_status_watch = 1;
1054
- }
1055831
1056832 netif_tx_wake_all_queues(be->vif->dev);
1057833
....@@ -1197,7 +973,187 @@
1197973 vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
1198974 "feature-ipv6-csum-offload", 0);
1199975
976
+ read_xenbus_frontend_xdp(be, dev);
977
+
1200978 return 0;
979
+}
980
+
981
+static int netback_remove(struct xenbus_device *dev)
982
+{
983
+ struct backend_info *be = dev_get_drvdata(&dev->dev);
984
+
985
+ unregister_hotplug_status_watch(be);
986
+ xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
987
+ if (be->vif) {
988
+ kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
989
+ backend_disconnect(be);
990
+ xenvif_free(be->vif);
991
+ be->vif = NULL;
992
+ }
993
+ kfree(be->hotplug_script);
994
+ kfree(be);
995
+ dev_set_drvdata(&dev->dev, NULL);
996
+ return 0;
997
+}
998
+
999
+/**
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;
12011157 }
12021158
12031159 static const struct xenbus_device_id netback_ids[] = {
....@@ -1211,6 +1167,7 @@
12111167 .remove = netback_remove,
12121168 .uevent = netback_uevent,
12131169 .otherend_changed = frontend_changed,
1170
+ .allow_rebind = true,
12141171 };
12151172
12161173 int xenvif_xenbus_init(void)