Beispiel #1
0
void grants_init(void)
{
	unsigned long frames[NR_GRANT_PAGES];

	gnttab_setup_table_t op;
	op.dom = DOMID_SELF;
	op.nr_frames = NR_GRANT_PAGES;
	set_xen_guest_handle(op.frame_list, frames);
	int rs = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &op, 1);
	if (rs < 0)
		fatal_error("grants_init: setup_table failed: %d\n", rs);

	for (int i = NR_GRANT_ENTRIES-1; i >= NR_RESERVED_ENTRIES; i--)
	{
		free_list[i] = free_entry;
		free_entry = i;
	}

	grant_entries = mm_alloc_pages(NR_GRANT_PAGES);
	if (grant_entries == 0)
		fatal_error("grants_init: grant entries page allocation failed\n");
	for (int i = 0; i < NR_GRANT_PAGES; i++)
	{
		unsigned long ma_grant_table = frames[i] << PAGE_SHIFT;
		rs = HYPERVISOR_update_va_mapping((unsigned long)grant_entries + i*PAGE_SIZE,
			__pte(ma_grant_table | 7), UVMF_INVLPG);
		if (rs < 0)
			fatal_error("grants_init: update mapping failed: %d\n", rs);
	}
}
Beispiel #2
0
void netfe_init(void)
{
	int index = 0;
	netfe_t **link = &net_front_ends;

	while (1)
	{
		int n;
		char xs_key[256];
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/backend-id", index);
		int rs = xenstore_read_int(&n, xs_key);
		if (rs != 0)
			break;

		// FE/(index) is present
		domid_t backend_id = (domid_t)n;

		netfe_t *fe = (netfe_t *)mm_alloc_pages(PSIZE(sizeof(netfe_t)));
		memset(fe, 0, sizeof(*fe));
		
		// setup shared rings
		fe->rxs = (netif_rx_sring_t *)mm_alloc_page();
		assert(fe->rxs != 0);
		fe->txs = (netif_tx_sring_t *)mm_alloc_page();
		assert(fe->txs != 0);

		SHARED_RING_INIT(fe->rxs);
		SHARED_RING_INIT(fe->txs);
	
		FRONT_RING_INIT(&fe->rx_ring, fe->rxs, PAGE_SIZE);
		FRONT_RING_INIT(&fe->tx_ring, fe->txs, PAGE_SIZE);
	
		grants_allow_access(&fe->rx_ref, backend_id, virt_to_mfn(fe->rxs));
		grants_allow_access(&fe->tx_ref, backend_id, virt_to_mfn(fe->txs));

		// set up receive buffers
		for (int i = 0; i < NR_RX_BUFFERS; i++)
		{
			fe->rx_buffers[i] = mm_alloc_page();
			assert(fe->rx_buffers[i] != 0);
			unsigned long mfn = virt_to_mfn(fe->rx_buffers[i]);
			grants_allow_access(&fe->rx_buf_refs[i], backend_id, mfn);
		}
	
		// set up send buffers
		fe->free_tx_head = NO_TX_BUFFER;
		for (int i = 0; i < NR_TX_BUFFERS; i++)
		{
			fe->tx_buffers[i] = mm_alloc_page();
			assert(fe->tx_buffers[i] != 0);
			unsigned long mfn = virt_to_mfn(fe->tx_buffers[i]);
			grants_allow_access(&fe->tx_buf_refs[i], backend_id, mfn);

			fe->free_tx_bufs[i] = fe->free_tx_head;
			fe->free_tx_head = i;
		}
	
		// set up interrupt
		fe->evtchn = event_alloc_unbound(backend_id);
		event_bind(fe->evtchn, netfe_int, (void *)fe);
	
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/rx-ring-ref", index);
		rs = xenstore_write_uint(xs_key, fe->rx_ref);
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/tx-ring-ref", index);
		rs = xenstore_write_uint(xs_key, fe->tx_ref);
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/event-channel", index);
		rs = xenstore_write_uint(xs_key, fe->evtchn);
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/request-rx-copy", index);
		rs = xenstore_write(xs_key, "1");
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/feature-no-csum-offload", index);
		rs = xenstore_write(xs_key, "1");
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/feature-rx-notify", index);
		rs = xenstore_write(xs_key, "1");
		assert(rs == 0);
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/state", index);
		rs = xenstore_write(xs_key, "4");	// XenbusStateConnected
		assert(rs == 0);

		// read MAC address
		char buf[64];
		snprintf(xs_key, sizeof(xs_key), "device/vif/%d/mac", index);
		rs = xenstore_read(xs_key, buf, sizeof(buf));
		assert(rs == 0);
		rs = parse_mac(buf, fe->mac);
		assert(rs == 0);

		fe->mac_len = ETH_ALEN;
		printk("\reth%d: MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", index,
					fe->mac[0], fe->mac[1], fe->mac[2],
					fe->mac[3], fe->mac[4], fe->mac[5]);

		//
		// Publish EXT_RX_BUFFERS requests only and replenish then to this number
		// during each interrupt handler invocation.
		//
		for (int i = 0; i < EXT_RX_BUFFERS; i++)
		{
			netif_rx_request_t *req = RING_GET_REQUEST(&fe->rx_ring, fe->rx_ring.req_prod_pvt);
			req->id = i; //rx_id++;
			req->gref = fe->rx_buf_refs[i];
			fe->rx_ring.req_prod_pvt++;
		}

		RING_PUSH_REQUESTS(&fe->rx_ring);
		event_kick(fe->evtchn);	

		fe->index = index++;
		//fe->next = 0;

		//fe->attached_lwip_netif = 0;
		//fe->attached_outlet = 0;

		// add to net_front_ends list
		*link = fe;
		link = &fe->next;
	}

	num_net_front_ends = index;
}