VOL-1119 BBSim - DHCP emulation

Change-Id: Iaa1a046715c7de99297246b4842784ef16c57acb
diff --git a/core/core_server.go b/core/core_server.go
index 39f4ca7..9e609c5 100644
--- a/core/core_server.go
+++ b/core/core_server.go
@@ -198,6 +198,31 @@
 				errchan <- err
 				return
 			}
+
+			if s.Mode == BOTH {
+				go func() {
+					defer func() {
+						log.Println("exeDHCPTest Done")
+					}()
+					info, err := s.identifyNniIoinfo("outside")
+					setup.ActivateDHCPServer(info.name, s.DhcpServerIP)
+
+					infos, err := s.getUniIoinfos("outside")
+					if err != nil {
+						errchan <- err
+						return
+					}
+					univeths := []string{}
+					for _, info := range infos {
+						univeths = append(univeths, info.name)
+					}
+					err = s.exeDHCPTest(univeths)
+					if err != nil {
+						errchan <- err
+						return
+					}
+				}()
+			}
 		}()
 		wg.Wait()
 		tearDown(vethenv) // Grace teardown
@@ -315,6 +340,25 @@
 	return nil
 }
 
+func (s *Server) exeDHCPTest(vethenv []string) error {
+	log.Println("exeDHCPTest Start")
+	for i := 0; i < s.DhcpWait; i++ {
+		select {
+		case <-s.Endchan:
+			log.Println("exeDHCPTest thread receives close !")
+			return nil
+		default:
+			log.Println("exeDHCPTest is now sleeping....")
+			time.Sleep(time.Second)
+		}
+	}
+	err := setup.ActivateDHCPClients(vethenv)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
 func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
 	layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
 	if layerEth != nil {
diff --git a/core/io_worker.go b/core/io_worker.go
index a535869..d512b01 100644
--- a/core/io_worker.go
+++ b/core/io_worker.go
@@ -73,8 +73,8 @@
 			return retpkt, vid, nil
 		}
 	}
-	//return pkt, 1, nil
-	return nil, 0, errors.New("failed to pop vlan")
+	return pkt, 0, nil
+	//return nil, 0, errors.New("failed to pop vlan")
 }
 
 func PushVLAN(pkt gopacket.Packet, vid uint16) (gopacket.Packet, error) {
diff --git a/setup/setup_env.go b/setup/setup_env.go
index a4210f5..c391875 100644
--- a/setup/setup_env.go
+++ b/setup/setup_env.go
@@ -37,6 +37,15 @@
 	return nil
 }
 
+func ActivateDHCPClients(vethnames []string) error {
+	for _, vethname := range vethnames {
+		if err := activateDHCPClient(vethname); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
 func KillAllWPASups() error {
 	err := exec.Command("pkill", "wpa_supplicant").Run()
 	if err != nil {
@@ -107,4 +116,33 @@
 	}
 	log.Printf("activateWPASupplicant() for :%s\n", vethname)
 	return
-}
\ No newline at end of file
+}
+
+func activateDHCPClient(vethname string) (err error) {
+	cmd := "/usr/local/bin/dhclient"
+	err = exec.Command(cmd, vethname).Start()
+	if err != nil {
+		log.Printf("[ERROR] Faile to activateWPASupplicant() for :%s %v\n", vethname, err)
+		return
+	}
+	log.Printf("activateDHCPClient()\n", vethname)
+	return
+}
+
+func ActivateDHCPServer(veth string, serverip string) {
+	cmd := "/sbin/ifconfig"
+	err := exec.Command(cmd, veth, serverip, "up").Run()
+	if err != nil {
+		log.Printf("[ERROR] Fail to up %s()\n", veth, err)
+		return
+	}
+	cmd = "/usr/local/bin/dhcpd"
+	conf := "/etc/dhcp/dhcpd.conf"
+	err = exec.Command(cmd, "-cf", conf, veth).Run()
+	if err != nil {
+		log.Printf("[ERROR] Fail to activateDHCP Server ()\n", err)
+		return
+	}
+	log.Printf("Activate DHCP Server()\n")
+	return
+}