001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.anarres.qemu.exec.recipe;
006
007import javax.annotation.Nonnull;
008import org.anarres.qemu.exec.QEmuDeviceOption;
009import org.anarres.qemu.exec.QEmuNetdevOption;
010import org.anarres.qemu.exec.util.QEmuIdAllocator;
011import org.anarres.qemu.exec.util.QEmuOptionsList;
012
013/**
014 *
015 * @author shevek
016 */
017public class QEmuVirtioNetRecipe extends QEmuOptionsList implements QEmuRecipe {
018
019    // tap,fd=27,id=hostnet0,vhost=on,vhostfd=28
020    public final QEmuNetdevOption.Tap netdevOption;
021    // virtio-net-pci,netdev=hostnet0,id=net0,mac=fa:16:3e:13:ff:1f,bus=pci.0,addr=0x3
022    public final QEmuDeviceOption.VirtioNet deviceOption;
023
024    /**
025     * Call new QEmuVirtioNetRecipe(line.getAllocator(), "tap0");
026     *
027     * @param allocator
028     * @param ifname
029     */
030    public QEmuVirtioNetRecipe(@Nonnull QEmuIdAllocator allocator, @Nonnull String ifname) {
031        int id = allocator.newNetworkIndex();
032        netdevOption = new QEmuNetdevOption.Tap();
033        netdevOption
034                .withTapInterface(ifname)
035                .withId("backend-net-" + id);
036        add(netdevOption);
037        deviceOption = new QEmuDeviceOption.VirtioNet();
038        deviceOption
039                .withId("virtio-net-" + id)
040                // .withPciAddress(allocator)   // Don't do this - let qemu autoallocate.
041                .withProperty(QEmuDeviceOption.VirtioNet.PROP_NETDEV, netdevOption.id);
042        add(deviceOption);
043    }
044
045    @Nonnull
046    public QEmuVirtioNetRecipe withMac(@Nonnull String mac) {
047        deviceOption.withMac(mac);
048        return this;
049    }
050
051    @Nonnull
052    public QEmuVirtioNetRecipe withPciAddress(@Nonnull String address) {
053        deviceOption.withPciAddress(address);
054        return this;
055    }
056
057    @Nonnull
058    public QEmuVirtioNetRecipe withPciAddress(@Nonnull QEmuIdAllocator allocator) {
059        deviceOption.withPciAddress(allocator);
060        return this;
061    }
062
063    @Nonnull
064    public QEmuVirtioNetRecipe withTapInterface(@Nonnull String ifname) {
065        netdevOption.withTapInterface(ifname);
066        return this;
067    }
068
069    @Nonnull
070    public QEmuVirtioNetRecipe withProperty(@Nonnull String key, @Nonnull String value) {
071        deviceOption.withProperty(key, value);
072        return this;
073    }
074}