openvswitch2.11.0修改源码后重新编译(2) SDN实验---使用git安装Mininet

一:前提

已经正常安装了SDN环境(mininet和openswitch2.11.0和Ryu)

使用前面教程安装环境SDN实验---使用git安装Mininet

(一)测试ovs是否正常使用

1.ryu启动

2.mn启动测试

二:原始信息

三:实验开始

(一)修改源码:在vport.c和.h中添加代码

1.vport.h文件

//custom function to log info
void send_msg(const char* filename,const char* info);
//file operation function 
struct file *file_open(const char *path, int flags, int rights);

void file_close(struct file *file);

int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size);

int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size);

2.vport.c文件

//custom function to log info
void send_msg(const char* filename,const char* info)
{
    loff_t f_pos;
    struct file* fp = file_open(filename,O_RDWR|O_CREAT,0666);
    f_pos = fp->f_pos;
    file_write(fp,f_pos,info,strlen(info)+1);
    file_close(fp);
}

//file operation function 
struct file *file_open(const char *path, int flags, int rights) 
{
    struct file *filp = NULL;
    mm_segment_t oldfs;
    int err = 0;

    oldfs = get_fs();
    set_fs(get_ds());
    filp = filp_open(path, flags, rights);
    set_fs(oldfs);
    if (IS_ERR(filp)) {
        err = PTR_ERR(filp);
        return NULL;
    }
    return filp;
}

void file_close(struct file *file) 
{
    filp_close(file, NULL);
}

int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) 
{
    mm_segment_t oldfs;
    int ret;

    oldfs = get_fs();
    set_fs(get_ds());

    ret = vfs_read(file, data, size, &offset);

    set_fs(oldfs);
    return ret;
}   

int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) 
{
    mm_segment_t oldfs;
    int ret;

    oldfs = get_fs();
    set_fs(get_ds());

    ret = vfs_write(file, data, size, &offset);

    set_fs(oldfs);
    return ret;
}

(二)重新编译前检查

1.查看内核模块openvswitch是否正在被占用(若是被占用,不能被安装)

lsmod | grep openvswitch

第一行,第三列为1则被占用,为0未被占用(此处未被占用)

2.若是被占用,此时使用sudo ovs-dpctl show查看内核,应该会有一个ovs-system的datapath

此时需要使用sudo ovs-dpctl del-dp ovs-system删除刚才看到的datapath。

3.使用sudo rmmod openvswitch来移除openvswitch内核模块,再使用lsmod|grep openvswitch应该就没有openvswitch字样。

(三)开始重新编译

1.进入openswitch文件夹中

2.启动boot.sh脚本

 

 3.make clean清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件。

因为我们之前安装openswitch编译过一次,我们需要先清除上一次的文件。
没有错误的时候不用clean,这样编译会快很多,编译器会自动编译变动的部份。
当排除错误时就应当clean一下(当出现莫名其妙的错误时,就要想到可能是没有clean导致的)---出错可以参考上一篇文章openvswitch2.11.0修改源码后重新编译

4.使用sudo ./configure --with-linux=/lib/modules/`uname -r`/build配置

./configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系,但并不是所有的tar包都是源代码的包,可以ls看看有没有configure这个文件,如果是二进制的包,解压后直接就能使用

5.进行编译sudo make

6.进行sudo make install

7.在编译内核模块之前,我们将之前的ko模块备份出去

查看ko文件位置 modinfo openvswitch

备份原始ko文件,删除原始文件

8.开始编译内核文件sudo make modules_install

lsmod | grep bridge    查看原始程序是否在运行(我们前面以及关闭了,这里应该没有输出信息)
lsmod | grep openvswitch

9.加载内核模块sudo modprobe openvswitch

使用lsmod | grep open查看是否加载了内核

查看内核信息

10.后面还有操作

modprobe -D openvswitch  进行内核模块插入

modinfo /lib/modules/4.15.0-20-generic/extra/openvswitch.ko 

原本在二中,没有这个extra文件夹和这些内核模块。在make modules_install后出现

原始源文件中,被删除了openswitch.ko,编译后没有在这个文件中出现

11.初始化数据库配置文件

sudo mkdir -p /usr/local/etc/openvswitch
sudo ovsdb-tool create /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
ls /usr/local/etc/openvswitch

四:后续问题

(一)使用mn进行测试,出现下面错误

*** Creating  network
*** Adding controller
*** Adding hosts:
h1 h2 
*** Adding switches:
ovs-vsctl: unix:/usr/local/var/run/openvswitch/db.sock: database connection failed (No such file or directory)
ovs-vsctl exited with code 1
*** Error connecting to ovs-db with ovs-vsctl
Make sure that Open vSwitch is installed, that ovsdb-server is running, and that
"ovs-vsctl show" works correctly.
You may wish to try "service openvswitch-switch start"

(二)网上查找解决方案

ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock --remote=db:Open_vSwitch,Open_vSwitch,manager_options --pidfile --detach
ovs-vsctl --no-wait init
ovs-vswitchd --pidfile --detach --log-file

(三)出现不能创建/usr/local/var/run/openvswitch/db.sock,没有这个文件或者目录

 

原本没有run目录,无法创建递归目录导致错误。我们使用sudo mkdir -p /usr/local/var/run/openvswitch/
解决问题

(四)将(三)上面的解决方案,设置为sh脚本

boot_ovs.sh就是,sudo chmod 777 ./boot_ovs.sh 
启动即可

(五)启动过程中,因为在虚拟机中启动,可能出现客户端禁用cpu,导致Ubuntu系统卡死

之前我给了2个cpu,一个2核。应该适当降低

五:测试结果

(一)开启mn

sudo mn --test pingall

(二)检查日志

猜你喜欢

转载自www.cnblogs.com/ssyfj/p/11901922.html