summaryrefslogtreecommitdiff
path: root/framework/net_device.py
blob: 6796269b2602a102db854381ac731414dc7c718e (plain)
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# BSD LICENSE
#
# Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of Intel Corporation nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import os
import re
from functools import wraps
import time


import settings
from crb import Crb
from settings import TIMEOUT


class NetDevice(object):

    """
    Abstract the device which is PF or VF.
    """

    def __init__(self, crb, bus_id, devfun_id):
        if not isinstance(crb, Crb):
            raise Exception("  Please input the instance of Crb!!!")
        self.crb = crb
        self.bus_id = bus_id
        self.devfun_id = devfun_id
        self.pci = bus_id + ':' + devfun_id
        self.pci_id = self.get_pci_id(bus_id, devfun_id)
        self.default_driver = settings.get_nic_driver(self.pci_id)

        if self.nic_is_pf():
            self.default_vf_driver = ''
        self.get_interface_name()
        self.socket = self.get_nic_socket()

    def __send_expect(self, cmds, expected, timeout=TIMEOUT, alt_session=True):
        """
        Wrap the crb`s session as private session for sending expect.
        """
        return self.crb.send_expect(cmds, expected, timeout=timeout, alt_session=alt_session)

    def __get_os_type(self):
        """
        Get OS type.
        """
        return self.crb.get_os_type()

    def nic_is_pf(self):
        """
        It is the method that you can check if the nic is PF.
        """
        return True

    def nic_has_driver(func):
        """
        Check if the NIC has a driver.
        """
        @wraps(func)
        def wrapper(*args, **kwargs):
            nic_instance = args[0]
            nic_instance.current_driver = nic_instance.get_nic_driver()
            if not nic_instance.current_driver:
                return ''
            return func(*args, **kwargs)
        return wrapper

    def get_nic_driver(self):
        """
        Get the NIC driver.
        """
        return self.crb.get_pci_dev_driver(self.bus_id, self.devfun_id)

    def get_nic_socket(self):
        """
        Get socket id of specified pci device.
        """
        get_nic_socket = getattr(
            self, 'get_nic_socket_%s' %
            self.__get_os_type())
        return get_nic_socket(self.bus_id, self.devfun_id)

    def get_nic_socket_linux(self, bus_id, devfun_id):
        command = ('cat /sys/bus/pci/devices/0000\:%s\:%s/numa_node' %
                   (bus_id, devfun_id))
        try:
            out = self.__send_expect(command, '# ')
            socket = int(out)
        except:
            socket = -1
        return socket

    def get_nic_socket_freebsd(self, bus_id, devfun_id):
        NotImplemented

    @nic_has_driver
    def get_interface_name(self):
        """
        Get interface name of specified pci device.
        Cal this function will update intf_name everytime
        """
        get_interface_name = getattr(
            self, 'get_interface_name_%s' %
            self.__get_os_type())
        out = get_interface_name(self.bus_id, self.devfun_id, self.current_driver)
        if "No such file or directory" in out:
            self.intf_name = 'N/A'
        else:
            self.intf_name = out

        return self.intf_name

    def get_interface_name_linux(self, bus_id, devfun_id, driver):
        """
        Get interface name of specified pci device on linux.
        """
        driver_alias = driver.replace('-', '_')
        try:
            get_interface_name_linux = getattr(
                self,
                'get_interface_name_linux_%s' %
                driver_alias)
        except Exception as e:
            generic_driver = 'generic'
            get_interface_name_linux = getattr(self,
                                               'get_interface_name_linux_%s' % generic_driver)

        return get_interface_name_linux(bus_id, devfun_id)

    def get_interface_name_linux_virtio_pci(self, bus_id, devfun_id):
        """
        Get virtio device interface name by the default way on linux.
        """
        command = 'ls --color=never /sys/bus/pci/devices/0000\:%s\:%s/virtio*/net' % (
            bus_id, devfun_id)
        return self.__send_expect(command, '# ')

    def get_interface_name_linux_generic(self, bus_id, devfun_id):
        """
        Get the interface name by the default way on linux.
        """
        command = 'ls --color=never /sys/bus/pci/devices/0000\:%s\:%s/net' % (
            bus_id, devfun_id)
        return self.__send_expect(command, '# ')

    def get_interface_name_freebsd(self, bus_id, devfun_id, driver):
        """
        Get interface name of specified pci device on Freebsd.
        """
        try:
            get_interface_name_freebsd = getattr(self,
                                                 'get_interface_name_freebsd_%s' % driver)
        except Exception as e:
            generic_driver = 'generic'
            get_interface_name_freebsd = getattr(self,
                                                 'get_interface_name_freebsd_%s' % generic_driver)

        return get_interface_name_freebsd(bus_id, devfun_id)

    def get_interface_name_freebsd_generic(self, bus_id, devfun_id):
        """
        Get the interface name by the default way on freebsd.
        """
        out = self.__send_expect("pciconf -l", "# ")
        rexp = r"(\w*)@pci0:%s" % bus_id
        pattern = re.compile(rexp)
        match = pattern.findall(out)
        if len(match) == 0:
            return "No such file"
        return match[0]

    @nic_has_driver
    def get_mac_addr(self):
        """
        Get mac address of specified pci device.
        """
        get_mac_addr = getattr(self, 'get_mac_addr_%s' % self.__get_os_type())
        out = get_mac_addr(self.intf_name, self.bus_id, self.devfun_id, self.current_driver)
        if "No such file or directory" in out:
            return 'N/A'
        else:
            return out

    def get_mac_addr_linux(self, intf, bus_id, devfun_id, driver):
        """
        Get mac address of specified pci device on linux.
        """
        driver_alias = driver.replace('-', '_')
        try:
            get_mac_addr_linux = getattr(
                self,
                'get_mac_addr_linux_%s' %
                driver_alias)
        except Exception as e:
            generic_driver = 'generic'
            get_mac_addr_linux = getattr(
                self,
                'get_mac_addr_linux_%s' %
                generic_driver)

        return get_mac_addr_linux(intf, bus_id, devfun_id, driver)

    def get_pci_id(self, bus_id, devfun_id):
        command = ('cat /sys/bus/pci/devices/0000\:%s\:%s/vendor' %
                   (bus_id, devfun_id))
        out = self.__send_expect(command, '# ')
        vender = out[2:]
        command = ('cat /sys/bus/pci/devices/0000\:%s\:%s/device' %
                   (bus_id, devfun_id))
        out = self.__send_expect(command, '# ')
        device = out[2:]
        return "%s:%s" % (vender, device)

    def get_mac_addr_linux_generic(self, intf, bus_id, devfun_id, driver):
        """
        Get MAC by the default way on linux.
        """
        command = ('cat /sys/bus/pci/devices/0000\:%s\:%s/net/%s/address' %
                   (bus_id, devfun_id, intf))
        return self.__send_expect(command, '# ')

    def get_mac_addr_linux_virtio_pci(self, intf, bus_id, devfun_id, driver):
        """
        Get MAC by the default way on linux.
        """
        virtio_cmd = ('ls /sys/bus/pci/devices/0000\:%s\:%s/ | grep --color=never virtio' %
                      (bus_id, devfun_id))
        virtio = self.__send_expect(virtio_cmd, '# ')

        command = ('cat /sys/bus/pci/devices/0000\:%s\:%s/%s/net/%s/address' %
                   (bus_id, devfun_id, virtio, intf))
        return self.__send_expect(command, '# ')

    def get_mac_addr_freebsd(self, intf, bus_id, devfun_id, driver):
        """
        Get mac address of specified pci device on Freebsd.
        """
        try:
            get_mac_addr_freebsd = getattr(
                self,
                'get_mac_addr_freebsd_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            get_mac_addr_freebsd = getattr(
                self,
                'get_mac_addr_freebsd_%s' %
                generic_driver)

        return get_mac_addr_freebsd(intf, bus_id, devfun_id)

    def get_mac_addr_freebsd_generic(self, intf, bus_id, devfun_id):
        """
        Get the MAC by the default way on Freebsd.
        """
        out = self.__send_expect('ifconfig %s' % intf, '# ')
        rexp = r"ether ([\da-f:]*)"
        pattern = re.compile(rexp)
        match = pattern.findall(out)
        return match[0]

    @nic_has_driver
    def get_ipv4_addr(self):
        """
        Get ipv4 address of specified pci device.
        """
        get_ipv4_addr = getattr(
            self, 'get_ipv4_addr_%s' % self.__get_os_type())
        return get_ipv4_addr(self.intf_name, self.currenct_driver)

    def get_ipv4_addr_linux(self, intf, driver):
        """
        Get ipv4 address of specified pci device on linux.
        """
        try:
            get_ipv4_addr_linux = getattr(self, 'get_ipv4_linux_%s' % driver)
        except Exception as e:
            generic_driver = 'generic'
            get_ipv4_addr_linux = getattr(
                self, 'get_ipv4_linux_%s' %
                generic_driver)

        return get_ipv4_addr_linux(intf, bus_id, devfun_id, driver)

    def get_ipv4_addr_linux_generic(self, intf):
        """
        Get IPv4 address by the default way on linux.
        """
        out = self.__send_expect("ip -family inet address show dev %s | awk '/inet/ { print $2 }'"
                                 % intf, "# ")
        return out.split('/')[0]

    def get_ipv4_addr_freebsd(self, intf, driver):
        """
        Get ipv4 address of specified pci device on Freebsd.
        """
        try:
            get_ipv4_addr_freebsd = getattr(
                self,
                'get_ipv4_addr_freebsd_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            get_ipv4_addr_freebsd = getattr(
                self,
                'get_ipv4_addr_freebsd_%s' %
                generic_driver)

        return get_ipv4_addr_freebsd(intf, bus_id, devfun_id)

    def get_ipv4_addr_freebsd_generic(self, intf):
        """
        Get the IPv4 address by the default way on Freebsd.
        """
        out = self.__send_expect('ifconfig %s' % intf, '# ')
        rexp = r"inet ([\d:]*)%"
        pattern = re.compile(rexp)
        match = pattern.findall(out)
        if len(match) == 0:
            return None

        return match[0]

    @nic_has_driver
    def enable_ipv6(self):
        """
        Enable ipv6 address of specified pci device.
        """
        if self.current_driver != self.default_driver:
            return

        enable_ipv6 = getattr(
            self, 'enable_ipv6_%s' % self.__get_os_type())
        return enable_ipv6(self.intf_name)

    def enable_ipv6_linux(self, intf):
        """
        Enable ipv6 address of specified pci device on linux.
        """
        self.__send_expect("sysctl net.ipv6.conf.%s.disable_ipv6=0" %
                           intf, "# ")
        # FVL interface need down and up for re-enable ipv6
        if self.default_driver == 'i40e':
            self.__send_expect("ifconfig %s down" % intf, "# ")
            self.__send_expect("ifconfig %s up" % intf, "# ")

    def enable_ipv6_freebsd(self, intf):
        pass

    @nic_has_driver
    def disable_ipv6(self):
        """
        Disable ipv6 address of specified pci device.
        """
        if self.current_driver != self.default_driver:
            return
        disable_ipv6 = getattr(
            self, 'disable_ipv6_%s' % self.__get_os_type())
        return disable_ipv6(self.intf_name)

    def disable_ipv6_linux(self, intf):
        """
        Disable ipv6 address of specified pci device on linux.
        """
        self.__send_expect("sysctl net.ipv6.conf.%s.disable_ipv6=1" %
                           intf, "# ")

    def disable_ipv6_freebsd(self, intf):
        pass

    @nic_has_driver
    def get_ipv6_addr(self):
        """
        Get ipv6 address of specified pci device.
        """
        get_ipv6_addr = getattr(
            self, 'get_ipv6_addr_%s' % self.__get_os_type())
        return get_ipv6_addr(self.intf_name, self.current_driver)

    @nic_has_driver
    def get_ipv6_addr(self):
        """
        Get ipv6 address of specified pci device.
        """
        get_ipv6_addr = getattr(
            self, 'get_ipv6_addr_%s' % self.__get_os_type())
        return get_ipv6_addr(self.intf_name, self.current_driver)

    def get_ipv6_addr_linux(self, intf, driver):
        """
        Get ipv6 address of specified pci device on linux.
        """
        try:
            get_ipv6_addr_linux = getattr(
                self,
                'get_ipv6_addr_linux_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            get_ipv6_addr_linux = getattr(
                self,
                'get_ipv6_addr_linux_%s' %
                generic_driver)

        return get_ipv6_addr_linux(intf)

    def get_ipv6_addr_linux_generic(self, intf):
        """
        Get the IPv6 address by the default way on linux.
        """
        out = self.__send_expect("ip -family inet6 address show dev %s | awk '/inet6/ { print $2 }'"
                                 % intf, "# ")
        return out.split('/')[0]

    def get_ipv6_addr_freebsd(self, intf, driver):
        """
        Get ipv6 address of specified pci device on Freebsd.
        """
        try:
            get_ipv6_addr_freebsd = getattr(
                self,
                'get_ipv6_addr_freebsd_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            get_ipv6_addr_freebsd = getattr(
                self,
                'get_ipv6_addr_freebsd_%s' %
                generic_driver)

        return get_ipv6_addr_freebsd(intf)

    def get_ipv6_addr_freebsd_generic(self, intf):
        """
        Get the IPv6 address by the default way on Freebsd.
        """
        out = self.__send_expect('ifconfig %s' % intf, '# ')
        rexp = r"inet6 ([\da-f:]*)%"
        pattern = re.compile(rexp)
        match = pattern.findall(out)
        if len(match) == 0:
            return None

        return match[0]

    def get_nic_numa(self):
        """
        Get numa number of specified pci device.
        """
        self.crb.get_nic_numa(self.bus_id, self.devfun_id)

    def get_card_type(self):
        """
        Get card type of specified pci device.
        """
        return self.crb.get_pci_dev_id(self.bus_id, self.devfun_id)

    @nic_has_driver
    def get_sriov_vfs_pci(self):
        """
        Get all SRIOV VF pci bus of specified pci device.
        """
        get_sriov_vfs_pci = getattr(
            self, 'get_sriov_vfs_pci_%s' % self.__get_os_type())
        return get_sriov_vfs_pci(self.bus_id, self.devfun_id, self.current_driver)

    def get_sriov_vfs_pci_linux(self, bus_id, devfun_id, driver):
        """
        Get all SRIOV VF pci bus of specified pci device on linux.
        """
        try:
            get_sriov_vfs_pci_linux = getattr(
                self,
                'get_sriov_vfs_pci_linux_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            get_sriov_vfs_pci_linux = getattr(
                self,
                'get_sriov_vfs_pci_linux_%s' %
                generic_driver)

        return get_sriov_vfs_pci_linux(bus_id, devfun_id)

    def get_sriov_vfs_pci_linux_generic(self, bus_id, devfun_id):
        """
        Get all the VF PCIs of specified PF by the default way on linux.
        """
        sriov_numvfs = self.__send_expect(
            "cat /sys/bus/pci/devices/0000\:%s\:%s/sriov_numvfs" %
            (bus_id, devfun_id), "# ")
        sriov_vfs_pci = []
        if int(sriov_numvfs) == 0:
            pass
        else:
            try:
                virtfns = self.__send_expect(
                    "ls -d /sys/bus/pci/devices/0000\:%s\:%s/virtfn*" %
                    (bus_id, devfun_id), "# ")
                for virtfn in virtfns.split():
                    vf_uevent = self.__send_expect(
                        "cat %s" %
                        os.path.join(virtfn, "uevent"), "# ")
                    vf_pci = re.search(
                        r"PCI_SLOT_NAME=0000:([0-9a-f]+:[0-9a-f]+\.[0-9a-f]+)",
                        vf_uevent).group(1)
                    sriov_vfs_pci.append(vf_pci)
            except Exception as e:
                print "Scan linux port [0000:%s.%s] sriov vf failed: %s" % (bus_id, devfun_id, e)

        return sriov_vfs_pci

    @nic_has_driver
    def generate_sriov_vfs(self, vf_num):
        """
        Generate some numbers of SRIOV VF.
        """
        if vf_num == 0:
            self.bind_vf_driver()
        generate_sriov_vfs = getattr(
            self, 'generate_sriov_vfs_%s' %
            self.__get_os_type())
        generate_sriov_vfs(
            self.bus_id,
            self.devfun_id,
            vf_num,
            self.current_driver)
        if vf_num != 0:
            self.sriov_vfs_pci = self.get_sriov_vfs_pci()

            vf_pci = self.sriov_vfs_pci[0]
            addr_array = vf_pci.split(':')
            bus_id = addr_array[0]
            devfun_id = addr_array[1]

            self.default_vf_driver = self.crb.get_pci_dev_driver(
                bus_id, devfun_id)
        else:
            self.sriov_vfs_pci = []
        time.sleep(1)

    def generate_sriov_vfs_linux(self, bus_id, devfun_id, vf_num, driver):
        """
        Generate some numbers of SRIOV VF.
        """
        try:
            generate_sriov_vfs_linux = getattr(
                self,
                'generate_sriov_vfs_linux_%s' %
                driver)
        except Exception as e:
            generic_driver = 'generic'
            generate_sriov_vfs_linux = getattr(
                self,
                'generate_sriov_vfs_linux_%s' %
                generic_driver)

        return generate_sriov_vfs_linux(bus_id, devfun_id, vf_num)

    def generate_sriov_vfs_linux_generic(self, bus_id, devfun_id, vf_num):
        """
        Generate SRIOV VFs by the default way on linux.
        """
        nic_driver = self.get_nic_driver()

        if not nic_driver:
            return None

        vf_reg_file = "sriov_numvfs"
        vf_reg_path = os.path.join("/sys/bus/pci/devices/0000:%s:%s" %
                                   (bus_id, devfun_id), vf_reg_file)
        self.__send_expect("echo %d > %s" %
                           (int(vf_num), vf_reg_path), "# ")

    def generate_sriov_vfs_linux_igb_uio(self, bus_id, devfun_id, vf_num):
        """
        Generate SRIOV VFs by the special way of igb_uio driver on linux.
        """
        nic_driver = self.get_nic_driver()

        if not nic_driver:
            return None

        vf_reg_file = "max_vfs"
        if self.default_driver == 'i40e':
            regx_reg_path = "find /sys -name %s | grep %s:%s" % (vf_reg_file, bus_id, devfun_id)
            vf_reg_path = self.__send_expect(regx_reg_path, "# ")
        else:
            vf_reg_path = os.path.join("/sys/bus/pci/devices/0000:%s:%s" %
                                       (bus_id, devfun_id), vf_reg_file)
        self.__send_expect("echo %d > %s" %
                           (int(vf_num), vf_reg_path), "# ")

    def destroy_sriov_vfs(self):
        """
        Destroy the SRIOV VFs.
        """
        self.generate_sriov_vfs(0)

    def bind_vf_driver(self, pci='', driver=''):
        """
        Bind the specified driver to VF.
        """
        bind_vf_driver = getattr(self, 'bind_driver_%s' % self.__get_os_type())
        if not driver:
            if not self.default_vf_driver:
                print "Must specify a driver because default VF driver is NULL!"
                return
            driver = self.default_vf_driver

        if not pci:
            if not self.sriov_vfs_pci:
                print "No VFs on the nic [%s]!" % self.pci
                return
            for vf_pci in self.sriov_vfs_pci:
                addr_array = vf_pci.split(':')
                bus_id = addr_array[0]
                devfun_id = addr_array[1]

                bind_vf_driver(bus_id, devfun_id, driver)
        else:
            addr_array = pci.split(':')
            bus_id = addr_array[0]
            devfun_id = addr_array[1]

            bind_vf_driver(bus_id, devfun_id, driver)

    def bind_driver(self, driver=''):
        """
        Bind specified driver to PF.
        """
        bind_driver = getattr(self, 'bind_driver_%s' % self.__get_os_type())
        if not driver:
            if not self.default_driver:
                print "Must specify a driver because default driver is NULL!"
                return
            driver = self.default_driver
        ret = bind_driver(self.bus_id, self.devfun_id, driver)
        time.sleep(1)
        return ret

    def bind_driver_linux(self, bus_id, devfun_id, driver):
        """
        Bind NIC port to specified driver on linux.
        """
        driver_alias = driver.replace('-', '_')
        try:
            bind_driver_linux = getattr(
                self,
                'bind_driver_linux_%s' %
                driver_alias)
            return bind_driver_linux(bus_id, devfun_id)
        except Exception as e:
            driver_alias = 'generic'
            bind_driver_linux = getattr(
                self,
                'bind_driver_linux_%s' %
                driver_alias)
            return bind_driver_linux(bus_id, devfun_id, driver)

    def bind_driver_linux_generic(self, bus_id, devfun_id, driver):
        """
        Bind NIC port to specified driver by the default way on linux.
        """
        new_id = self.pci_id.replace(':', ' ')
        nic_pci_num = ':'.join(['0000', bus_id, devfun_id])
        self.__send_expect(
            "echo %s > /sys/bus/pci/drivers/%s/new_id" % (new_id, driver), "# ")
        self.__send_expect(
            "echo %s > /sys/bus/pci/devices/0000\:%s\:%s/driver/unbind" %
            (nic_pci_num, bus_id, devfun_id), "# ")
        self.__send_expect(
            "echo %s > /sys/bus/pci/drivers/%s/bind" %
            (nic_pci_num, driver), "# ")

    def bind_driver_linux_pci_stub(self, bus_id, devfun_id):
        """
        Bind NIC port to the pci-stub driver on linux.
        """
        new_id = self.pci_id.replace(':', ' ')
        self.__send_expect(
            "echo %s > /sys/bus/pci/drivers/pci-stub/new_id" % new_id, "# ")
        self.__send_expect(
            "echo %s > /sys/bus/pci/devices/0000\:%s\:%s/driver/unbind" %
            (nic_pci_num, bus_id, devfun_id), "# ")
        self.__send_expect(
            "echo %s > /sys/bus/pci/drivers/pci-stub/bind" %
            nic_pci_num, "# ")

    @nic_has_driver
    def unbind_driver(self, driver=''):
        """
        Unbind driver.
        """
        unbind_driver = getattr(
            self, 'unbind_driver_%s' %
            self.__get_os_type())
        if not driver:
            driver = 'generic'
        ret = unbind_driver(self.bus_id, self.devfun_id, driver)
        time.sleep(1)
        return ret

    def unbind_driver_linux(self, bus_id, devfun_id, driver):
        """
        Unbind driver on linux.
        """
        driver_alias = driver.replace('-', '_')

        unbind_driver_linux = getattr(
            self, 'unbind_driver_linux_%s' % driver_alias)
        return unbind_driver_linux(bus_id, devfun_id)

    def unbind_driver_linux_generic(self, bus_id, devfun_id):
        """
        Unbind driver by the default way on linux.
        """
        nic_pci_num = ':'.join(['0000', bus_id, devfun_id])
        cmd = "echo %s > /sys/bus/pci/devices/0000\:%s\:%s/driver/unbind"
        self.send_expect(cmd % (nic_pci_num, bus_id, devfun_id), "# ")