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