<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.25in 1.0in 1.25in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<pre style="background:#EFF0F1;vertical-align:baseline"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Add the tap in linux to be recognized by linux: ip tuntap add dev int0 mode tap<o:p></o:p></span></pre>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Then add it to the ovs
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> ovs-discuss-bounces@openvswitch.org [mailto:ovs-discuss-bounces@openvswitch.org]
<b>On Behalf Of </b>sougol gheissi<br>
<b>Sent:</b> Sunday, 03 September, 2017 2:25 PM<br>
<b>To:</b> ovs-discuss@openvswitch.org<br>
<b>Subject:</b> [ovs-discuss] Tap interface in the OVS<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Hello all,<o:p></o:p></p>
</div>
<p class="MsoNormal">I want to capture the packets in the userspace, so I decided to use tap interface in the OVS. I added a tap interface using the command below:<o:p></o:p></p>
</div>
<pre style="margin-bottom:12.0pt"><code>#ovs-vsctl add-port br0 int0 -- set Interface int0 type=internal</code><o:p></o:p></pre>
<pre><code>then I tried to send the packets to that port by this OVS command:</code><o:p></o:p></pre>
<pre><code>#ovs-ofctl add-flow br0 table=0,priority=100,udp,tp_dst=5060,actions=output:5</code><o:p></o:p></pre>
<pre><code>(5 is the int0 port number)</code><br><code>then I write a piece of code to capture the packets,</code><br><br><code><o:p></o:p></code></pre>
<pre><code>#include <unistd.h><o:p></o:p></code></pre>
<pre><code>#include <fcntl.h><o:p></o:p></code></pre>
<pre><code>#include <stdlib.h><o:p></o:p></code></pre>
<pre><code>#include <stdio.h><o:p></o:p></code></pre>
<pre><code>#include <string.h><o:p></o:p></code></pre>
<pre><code>#include <syslog.h><o:p></o:p></code></pre>
<pre><code>#include <errno.h><o:p></o:p></code></pre>
<pre><code>#include <stdlib.h><o:p></o:p></code></pre>
<pre><code>#include <unistd.h><o:p></o:p></code></pre>
<pre><code>#include <sys/socket.h><o:p></o:p></code></pre>
<pre><code>#include <netinet/in.h><o:p></o:p></code></pre>
<pre><code>#include <netinet/tcp.h><o:p></o:p></code></pre>
<pre><code>#include <netinet/ip.h><o:p></o:p></code></pre>
<pre><code>#include <netpacket/packet.h><o:p></o:p></code></pre>
<pre><code>#include <stdio.h><o:p></o:p></code></pre>
<pre><code>#include <stdlib.h><o:p></o:p></code></pre>
<pre><code>#include <errno.h><o:p></o:p></code></pre>
<pre><code>#include <getopt.h><o:p></o:p></code></pre>
<pre><code>#include <string.h><o:p></o:p></code></pre>
<pre><code>#include <arpa/inet.h><o:p></o:p></code></pre>
<pre><code>#include <stdio.h><o:p></o:p></code></pre>
<pre><code>#include <sys/socket.h><o:p></o:p></code></pre>
<pre><code>#include <arpa/inet.h><o:p></o:p></code></pre>
<pre><code>#include <malloc.h><o:p></o:p></code></pre>
<pre><code>#include <net/ethernet.h><o:p></o:p></code></pre>
<pre><code>#include <sys/ioctl.h><o:p></o:p></code></pre>
<pre><code>#include <net/if.h><o:p></o:p></code></pre>
<pre><code>#include <string.h><o:p></o:p></code></pre>
<pre><code>#include <sys/types.h><o:p></o:p></code></pre>
<pre><code>#include <time.h><o:p></o:p></code></pre>
<pre><code>#include <sys/ioctl.h><o:p></o:p></code></pre>
<pre><code>#include <sys/socket.h><o:p></o:p></code></pre>
<pre><code>#include <linux/if.h><o:p></o:p></code></pre>
<pre><code>#include <linux/if_tun.h><o:p></o:p></code></pre>
<pre><code>#include <signal.h><o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>static int tap_open(char *dev)<o:p></o:p></code></pre>
<pre><code>{ struct ifreq ifr;<o:p></o:p></code></pre>
<pre><code> int fd;<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code> if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { return -1; }<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code> memset(&ifr, 0, sizeof(ifr));<o:p></o:p></code></pre>
<pre><code> ifr.ifr_flags = IFF_TAP | IFF_NO_PI;<o:p></o:p></code></pre>
<pre><code> if (*dev) strncpy(ifr.ifr_name, dev, IFNAMSIZ);<o:p></o:p></code></pre>
<pre><code> if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { goto failed; } <o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code> strcpy(dev, ifr.ifr_name); return fd;<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>failed:<o:p></o:p></code></pre>
<pre><code> close(fd); return -1;<o:p></o:p></code></pre>
<pre><code>}<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>int tapfd;<o:p></o:p></code></pre>
<pre><code>void signal_handler(int sig) { close(tapfd); exit(1); }<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>int main()<o:p></o:p></code></pre>
<pre><code>{ unsigned char buf[2000];<o:p></o:p></code></pre>
<pre><code> unsigned char name[20];<o:p></o:p></code></pre>
<pre><code> int i;<o:p></o:p></code></pre>
<pre><code> int numbytes=0;<o:p></o:p></code></pre>
<pre><code> <o:p></o:p></code></pre>
<pre><code> <o:p></o:p></code></pre>
<pre><code> strcpy(name,"inet0"); tapfd=tap_open(name); <o:p></o:p></code></pre>
<pre><code> if(tapfd<0) { printf("port open error!\n"); return 0; }<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>signal(SIGHUP,signal_handler);<o:p></o:p></code></pre>
<pre><code>signal(SIGTERM,signal_handler);<o:p></o:p></code></pre>
<pre><code>signal(SIGKILL,signal_handler);<o:p></o:p></code></pre>
<pre><code>signal(SIGSTOP,signal_handler);<o:p></o:p></code></pre>
<pre><code>signal(SIGINT,signal_handler);<o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code>repeat: <o:p></o:p></code></pre>
<pre><code> numbytes=read(tapfd, buf, 2000);<o:p></o:p></code></pre>
<pre><code> if(numbytes<=0) { usleep(100); goto repeat;}<o:p></o:p></code></pre>
<pre><code> <o:p></o:p></code></pre>
<pre><code> printf("\tTAP Data:");<o:p></o:p></code></pre>
<pre><code> for (i=0; i<numbytes; i++) printf("%02x:", buf[i]);<o:p></o:p></code></pre>
<pre><code> printf("\n\n");<o:p></o:p></code></pre>
<pre><code> <o:p></o:p></code></pre>
<pre><code> goto repeat; <o:p></o:p></code></pre>
<pre><code><o:p> </o:p></code></pre>
<pre><code> close(tapfd);<o:p></o:p></code></pre>
<pre><code> return 0;<o:p></o:p></code></pre>
<pre><code>}</code><o:p></o:p></pre>
<p class="MsoNormal">But I get "<code><span style="font-size:10.0pt">port open error!" which shows I can not open the fd. Can anyone help me how to add a tap interface with OVS?</span></code><o:p></o:p></p>
</div>
<p class="MsoNormal"><code><span style="font-size:10.0pt">Thanks in advance</span></code><o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>