1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Xen network backend - mac client edition.
*
* A driver that sits above an existing GLDv3/Nemo MAC driver and
* relays packets to/from that driver from/to a guest domain.
*/
#include "xnb.h"
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/strsubr.h>
#include <sys/mac.h>
#include <net/if.h>
#include <sys/dlpi.h>
#include <sys/pattr.h>
#include <xen/sys/xenbus_impl.h>
#include <xen/sys/xendev.h>
typedef struct xnbo {
mac_handle_t o_mh;
mac_rx_handle_t o_mrh;
const mac_txinfo_t *o_mtx;
mac_notify_handle_t o_mnh;
boolean_t o_running;
boolean_t o_promiscuous;
uint32_t o_hcksum_capab;
} xnbo_t;
static void xnbo_close_mac(xnbo_t *);
/*
* Packets from the peer come here. We pass them to the mac device.
*/
static void
xnbo_to_mac(xnb_t *xnbp, mblk_t *mp)
{
xnbo_t *xnbop = xnbp->x_flavour_data;
ASSERT(mp != NULL);
if (!xnbop->o_running) {
xnbp->x_stat_rx_too_early++;
goto fail;
}
mp = xnbop->o_mtx->mt_fn(xnbop->o_mtx->mt_arg, mp);
if (mp != NULL) {
xnbp->x_stat_mac_full++;
goto fail;
}
return;
fail:
freemsgchain(mp);
}
static mblk_t *
xnbo_cksum_from_peer(xnb_t *xnbp, mblk_t *mp, uint16_t flags)
{
xnbo_t *xnbop = xnbp->x_flavour_data;
ASSERT(mp->b_next == NULL);
if ((flags & NETTXF_csum_blank) != 0) {
/*
* It would be nice to ASSERT that xnbp->x_cksum_offload
* is TRUE here, but some peers insist on assuming
* that it is available even when they have been told
* otherwise.
*
* The checksum in the packet is blank. Determine
* whether we can do hardware offload and, if so,
* update the flags on the mblk according. If not,
* calculate and insert the checksum using software.
*/
mp = xnb_process_cksum_flags(xnbp, mp,
xnbop->o_hcksum_capab);
}
return (mp);
}
static uint16_t
xnbo_cksum_to_peer(xnb_t *xnbp, mblk_t *mp)
{
uint16_t r = 0;
/*
* We might also check for HCK_PARTIALCKSUM here and,
* providing that the partial checksum covers the TCP/UDP
* payload, return NETRXF_data_validated.
*
* It seems that it's probably not worthwhile, as even MAC
* devices which advertise HCKSUM_INET_PARTIAL in their
* capabilities tend to use HCK_FULLCKSUM on the receive side
* - they are actually saying that in the output path the
* caller must use HCK_PARTIALCKSUM.
*/
if (xnbp->x_cksum_offload) {
uint32_t pflags, csum;
/*
* XXPV dme: Pull in improved hcksum_retrieve() from
* Crossbow, which gives back the csum in the seventh
* argument for HCK_FULLCKSUM.
*/
hcksum_retrieve(mp, NULL, NULL, NULL, NULL,
NULL, NULL, &pflags);
csum = DB_CKSUM16(mp);
/*
* If the MAC driver has asserted that the checksum is
* good, let the peer know.
*/
if (((pflags & HCK_FULLCKSUM) != 0) &&
(((pflags & HCK_FULLCKSUM_OK) != 0) ||
(csum == 0xffff)))
r |= NETRXF_data_validated;
}
return (r);
}
/*
* Packets from the mac device come here. We pass them to the peer.
*/
/*ARGSUSED*/
static void
xnbo_from_mac(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
{
xnb_t *xnbp = arg;
mp = xnb_to_peer(xnbp, mp);
if (mp != NULL)
freemsgchain(mp);
}
/*
* Packets from the mac device come here. We pass them to the peer if
* the destination mac address matches or it's a multicast/broadcast
* address.
*/
/*ARGSUSED*/
static void
xnbo_from_mac_filter(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
{
xnb_t *xnbp = arg;
xnbo_t *xnbop = xnbp->x_flavour_data;
mblk_t *next, *keep, *keep_head, *free, *free_head;
keep = keep_head = free = free_head = NULL;
#define ADD(list, bp) \
if (list != NULL) \
list->b_next = bp; \
else \
list##_head = bp; \
list = bp;
for (; mp != NULL; mp = next) {
mac_header_info_t hdr_info;
next = mp->b_next;
mp->b_next = NULL;
if (mac_header_info(xnbop->o_mh, mp, &hdr_info) != 0) {
ADD(free, mp);
continue;
}
if ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
(hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST)) {
ADD(keep, mp);
continue;
}
if (bcmp(hdr_info.mhi_daddr, xnbp->x_mac_addr,
sizeof (xnbp->x_mac_addr)) == 0) {
ADD(keep, mp);
continue;
}
ADD(free, mp);
}
#undef ADD
if (keep_head != NULL)
xnbo_from_mac(xnbp, mrh, keep_head);
if (free_head != NULL)
freemsgchain(free_head);
}
static void
xnbo_notify(void *arg, mac_notify_type_t type)
{
xnb_t *xnbp = arg;
xnbo_t *xnbop = xnbp->x_flavour_data;
switch (type) {
case MAC_NOTE_PROMISC:
xnbop->o_mtx = mac_tx_get(xnbop->o_mh);
break;
}
}
static boolean_t
xnbo_open_mac(xnb_t *xnbp, char *mac)
{
xnbo_t *xnbop = xnbp->x_flavour_data;
int err, need_rx_filter, need_setphysaddr, need_promiscuous;
const mac_info_t *mi;
char *xsname;
void (*rx_fn)(void *, mac_resource_handle_t, mblk_t *);
xsname = xvdi_get_xsname(xnbp->x_devinfo);
if ((err = mac_open(mac, &xnbop->o_mh)) != 0) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"cannot open mac device %s (%d)", mac, err);
return (B_FALSE);
}
ASSERT(xnbop->o_mh != NULL);
mi = mac_info(xnbop->o_mh);
ASSERT(mi != NULL);
if (mi->mi_media != DL_ETHER) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"mac device is not DL_ETHER (%d)", mi->mi_media);
xnbo_close_mac(xnbop);
return (B_FALSE);
}
if (mi->mi_media != mi->mi_nativemedia) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"mac device media and native media mismatch (%d != %d)",
mi->mi_media, mi->mi_nativemedia);
xnbo_close_mac(xnbop);
return (B_FALSE);
}
if (mi->mi_sdu_max > XNBMAXPKT) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"mac device SDU too big (%d)", mi->mi_sdu_max);
xnbo_close_mac(xnbop);
return (B_FALSE);
}
xnbop->o_mnh = mac_notify_add(xnbop->o_mh, xnbo_notify, xnbp);
ASSERT(xnbop->o_mnh != NULL);
/*
* Should the receive path filter packets from the downstream
* NIC before passing them to the peer? The default is "no".
*/
if (xenbus_scanf(XBT_NULL, xsname,
"SUNW-need-rx-filter", "%d", &need_rx_filter) != 0)
need_rx_filter = 0;
if (need_rx_filter > 0)
rx_fn = xnbo_from_mac_filter;
else
rx_fn = xnbo_from_mac;
xnbop->o_mrh = mac_rx_add(xnbop->o_mh, rx_fn, xnbp);
ASSERT(xnbop->o_mrh != NULL);
xnbop->o_mtx = mac_tx_get(xnbop->o_mh);
ASSERT(xnbop->o_mtx != NULL);
if (!mac_capab_get(xnbop->o_mh, MAC_CAPAB_HCKSUM,
&xnbop->o_hcksum_capab))
xnbop->o_hcksum_capab = 0;
/*
* Should we set the physical address of the underlying NIC
* to match that assigned to the peer? The default is "no".
*/
if (xenbus_scanf(XBT_NULL, xsname,
"SUNW-need-set-physaddr", "%d", &need_setphysaddr) != 0)
need_setphysaddr = 0;
if (need_setphysaddr > 0) {
struct ether_addr ea;
err = mac_unicst_set(xnbop->o_mh, xnbp->x_mac_addr);
/* Warn, but continue on. */
if (err != 0) {
bcopy(xnbp->x_mac_addr, ea.ether_addr_octet,
ETHERADDRL);
cmn_err(CE_WARN, "xnbo_open_mac: "
"cannot set MAC address of %s to "
"%s: %d", mac, ether_sprintf(&ea),
err);
}
}
/*
* Should we set the underlying NIC into promiscuous mode? The
* default is "no".
*/
if (xenbus_scanf(XBT_NULL, xsname,
"SUNW-need-promiscuous", "%d", &need_promiscuous) != 0)
need_promiscuous = 0;
if (need_promiscuous > 0) {
err = mac_promisc_set(xnbop->o_mh, B_TRUE, MAC_DEVPROMISC);
if (err != 0) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"cannot enable promiscuous mode of %s: %d",
mac, err);
xnbo_close_mac(xnbop);
return (B_FALSE);
}
xnbop->o_promiscuous = B_TRUE;
}
if ((err = mac_start(xnbop->o_mh)) != 0) {
cmn_err(CE_WARN, "xnbo_open_mac: "
"cannot start mac device (%d)", err);
xnbo_close_mac(xnbop);
return (B_FALSE);
}
xnbop->o_running = B_TRUE;
return (B_TRUE);
}
/*
* xnb calls back here when the user-level hotplug code reports that
* the hotplug has successfully completed. For this flavour that means
* that the underlying MAC device that we will use is ready to be
* opened.
*/
static boolean_t
xnbo_hotplug(xnb_t *xnbp)
{
char *xsname;
char mac[LIFNAMSIZ];
xsname = xvdi_get_xsname(xnbp->x_devinfo);
if (xenbus_scanf(XBT_NULL, xsname, "nic", "%s", mac) != 0) {
cmn_err(CE_WARN, "xnbo_hotplug: "
"cannot read nic name from %s", xsname);
return (B_FALSE);
}
return (xnbo_open_mac(xnbp, mac));
}
static void
xnbo_close_mac(xnbo_t *xnbop)
{
if (xnbop->o_mh == NULL)
return;
if (xnbop->o_running) {
mac_stop(xnbop->o_mh);
xnbop->o_running = B_FALSE;
}
if (xnbop->o_promiscuous) {
(void) mac_promisc_set(xnbop->o_mh, B_FALSE,
MAC_DEVPROMISC);
xnbop->o_promiscuous = B_FALSE;
}
xnbop->o_mtx = NULL;
if (xnbop->o_mrh != NULL) {
mac_rx_remove(xnbop->o_mh, xnbop->o_mrh, B_TRUE);
xnbop->o_mrh = NULL;
}
if (xnbop->o_mnh != NULL) {
mac_notify_remove(xnbop->o_mh, xnbop->o_mnh);
xnbop->o_mnh = NULL;
}
mac_close(xnbop->o_mh);
xnbop->o_mh = NULL;
}
/*
* xnb calls back here when we successfully synchronize with the
* driver in the guest domain. In this flavour there is nothing to do as
* we open the underlying MAC device on successful hotplug completion.
*/
/*ARGSUSED*/
static void
xnbo_connected(xnb_t *xnbp)
{
}
/*
* xnb calls back here when the driver in the guest domain has closed
* down the inter-domain connection. We close the underlying MAC device.
*/
static void
xnbo_disconnected(xnb_t *xnbp)
{
xnbo_close_mac(xnbp->x_flavour_data);
}
static int
xnbo_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
static xnb_flavour_t flavour = {
xnbo_to_mac, xnbo_connected, xnbo_disconnected, xnbo_hotplug,
xnbo_cksum_from_peer, xnbo_cksum_to_peer,
};
xnbo_t *xnbop;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
xnbop = kmem_zalloc(sizeof (*xnbop), KM_SLEEP);
xnbop->o_mh = NULL;
xnbop->o_mrh = NULL;
xnbop->o_mtx = NULL;
xnbop->o_running = B_FALSE;
xnbop->o_hcksum_capab = 0;
if (xnb_attach(dip, &flavour, xnbop) != DDI_SUCCESS) {
kmem_free(xnbop, sizeof (*xnbop));
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
xnbo_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
xnb_t *xnbp = ddi_get_driver_private(dip);
xnbo_t *xnbop = xnbp->x_flavour_data;
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
mutex_enter(&xnbp->x_tx_lock);
mutex_enter(&xnbp->x_rx_lock);
if (!xnbp->x_detachable || xnbp->x_connected ||
(xnbp->x_rx_buf_count > 0)) {
mutex_exit(&xnbp->x_rx_lock);
mutex_exit(&xnbp->x_tx_lock);
return (DDI_FAILURE);
}
mutex_exit(&xnbp->x_rx_lock);
mutex_exit(&xnbp->x_tx_lock);
xnbo_close_mac(xnbop);
kmem_free(xnbop, sizeof (*xnbop));
xnb_detach(dip);
return (DDI_SUCCESS);
}
static struct cb_ops cb_ops = {
nulldev, /* open */
nulldev, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
nodev, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
0, /* streamtab */
D_NEW | D_MP | D_64BIT /* Driver compatibility flag */
};
static struct dev_ops ops = {
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
nulldev, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
xnbo_attach, /* devo_attach */
xnbo_detach, /* devo_detach */
nodev, /* devo_reset */
&cb_ops, /* devo_cb_ops */
(struct bus_ops *)0, /* devo_bus_ops */
NULL /* devo_power */
};
static struct modldrv modldrv = {
&mod_driverops, "xnbo driver %I%", &ops,
};
static struct modlinkage modlinkage = {
MODREV_1, &modldrv, NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
|