import os, logging
from autotest_lib.client.bin import test, utils
from autotest_lib.client.common_lib import error

class pktgen(test.test):
    version = 1

    def execute(self, eth='eth0', count=50000, clone_skb=1,
                dst_ip='192.168.210.210', dst_mac='01:02:03:04:05:07',
                num_iterations=1):
        if not os.path.exists('/proc/net/pktgen'):
            utils.system('/sbin/modprobe pktgen')
        if not os.path.exists('/proc/net/pktgen'):
            raise error.TestError('pktgen not loaded')

        for i in xrange(num_iterations):
            logging.info('Adding %s (iteration %d)' % (eth, i))
            self.pgdev = '/proc/net/pktgen/kpktgend_0'

            self.pgset('rem_device_all')
            self.pgset('add_device ' + eth)
            self.pgset('max_before_softirq 10000')

            # Configure the individual devices
            logging.info('Configuring %s (iteration %d)' % (eth, i))

            self.ethdev='/proc/net/pktgen/' + eth
            self.pgdev=self.ethdev

            if clone_skb:
                self.pgset('clone_skb %d' % (count))
            self.pgset('min_pkt_size 60')
            self.pgset('max_pkt_size 60')
            self.pgset('dst ' + dst_ip)
            self.pgset('dst_mac ' + dst_mac)
            self.pgset('count %d' % (count))

            # Time to run
            self.pgdev='/proc/net/pktgen/pgctrl'
            self.pgset('start')

            output = os.path.join(self.resultsdir, eth)
            logging.info('Completed %s (iteration %d)' % (eth, i))
            utils.system('cp %s %s_%05d' % (self.ethdev, output, i))

    def pgset(self, command):
        file = open(self.pgdev, 'w')
        file.write(command + '\n');
        file.close

        if not utils.grep('Result: OK', self.pgdev):
            if not utils.grep('Result: NA', self.pgdev):
                utils.system('cat ' + self.pgdev)