Skip to content

Commit 6c489f8

Browse files
committed
feat: config internal port mac with hashed br name
Signed-off-by: Changliang Wu <changliang.wu@smartx.com>
1 parent feb6492 commit 6c489f8

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

ofctrl/ofctrl.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,18 +382,30 @@ func setDatapathID(conn *ovsdb.OvsdbClient, bridgeName string) error {
382382
h := sha256.New()
383383
h.Write([]byte(bridgeName))
384384
datapathID := h.Sum(nil)[:8]
385+
mac := net.HardwareAddr(h.Sum(nil)[:6])
386+
mac[0] &^= 1
385387

386388
config, _ := ovsdb.NewOvsMap(map[string]string{"datapath-id": hex.EncodeToString(datapathID)})
387-
operation := ovsdb.Operation{
389+
operDpid := ovsdb.Operation{
388390
Op: "mutate",
389391
Table: "Bridge",
390392
Where: []interface{}{[]interface{}{"name", "==", bridgeName}},
391393
Mutations: []interface{}{[]interface{}{"other_config", "insert", config}}, // never update the datapath id
392394
}
393-
_, err := ovsdbTransact(conn, "Open_vSwitch", operation)
394395

396+
operMac := ovsdb.Operation{
397+
Op: "mutate",
398+
Table: "Port",
399+
Where: []interface{}{[]interface{}{"name", "==", bridgeName}},
400+
Mutations: []interface{}{[]interface{}{"mac", "insert", mac.String()}},
401+
}
402+
if _, err := ovsdbTransact(conn, "Open_vSwitch", operDpid, operMac); err != nil {
403+
return err
404+
}
405+
406+
log.Infof("bridge %s internal port mac (if exist) has been set to %s", bridgeName, mac.String())
395407
log.Infof("bridge %s datapath id has been set to %s", bridgeName, hex.EncodeToString(datapathID))
396-
return err
408+
return nil
397409
}
398410

399411
func ovsdbTransact(client *ovsdb.OvsdbClient, database string, operation ...ovsdb.Operation) ([]ovsdb.OperationResult, error) {

ofctrl/ofctrl_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ func TestNewOFController(t *testing.T) {
859859
driver := ovsdbDriver.NewOvsDriver(bridgeName)
860860
defer driver.Delete()
861861

862+
if err := driver.CreatePort(bridgeName, "internal", 0); err != nil {
863+
t.Fatalf("fail to create default internal port : %s", err)
864+
}
865+
862866
_ = NewOFController(&ofActor, uint16(rand.Intn(1024)), driver.OVSClient(), bridgeName)
863867

864868
//wait for 2sec
@@ -872,4 +876,8 @@ func TestNewOFController(t *testing.T) {
872876
if config["datapath-id"] == "" {
873877
t.Fatalf("datapath id must be set before connect to switch")
874878
}
879+
880+
if mac, err := driver.GetInternalPortMac(); err != nil || mac == "" {
881+
t.Fatalf("internal port mac must be set before connect to switch, err = %s", err)
882+
}
875883
}

ovsdbDriver/ovsdbDriver.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ func NewOvsDriver(bridgeName string) *OvsDriver {
4848
_ = ovs.MonitorAll("Open_vSwitch", "")
4949

5050
// Create the default bridge instance
51-
err = ovsDriver.CreateBridge(ovsDriver.OvsBridgeName)
52-
if err != nil {
51+
if err = ovsDriver.CreateBridge(ovsDriver.OvsBridgeName); err != nil {
5352
log.Fatalf("Error creating the default bridge. Err: %v", err)
5453
}
5554

@@ -183,9 +182,9 @@ func (self *OvsDriver) ovsdbTransact(ops []libovsdb.Operation) error {
183182
// Parse reply and look for errors
184183
for i, o := range reply {
185184
if o.Error != "" && i < len(ops) {
186-
return errors.New("OVS Transaction failed err " + o.Error + "Details: " + o.Details)
185+
return errors.New("OVS Transaction failed err " + o.Error + "Details: " + o.Details + " UUID: " + o.UUID.GoUuid)
187186
} else if o.Error != "" {
188-
return errors.New("OVS Transaction failed err " + o.Error + "Details: " + o.Details)
187+
return errors.New("OVS Transaction failed err " + o.Error + "Details: " + o.Details + " UUID: " + o.UUID.GoUuid)
189188
}
190189
}
191190

@@ -345,6 +344,31 @@ func (self *OvsDriver) GetOtherConfig() (map[string]string, error) {
345344
return buildMapFromOVSDBMap(externalIds), nil
346345
}
347346

347+
func (self *OvsDriver) GetInternalPortMac() (string, error) {
348+
selectOper := libovsdb.Operation{
349+
Op: "select",
350+
Table: "Port",
351+
Where: []interface{}{[]interface{}{"name", "==", self.OvsBridgeName}},
352+
Columns: []string{"mac"},
353+
}
354+
355+
opers := []libovsdb.Operation{selectOper}
356+
ret, err := self.ovsClient.Transact("Open_vSwitch", opers...)
357+
if err != nil {
358+
return "", fmt.Errorf("ovsdb select internal port mac transaction failed: %v", opers)
359+
}
360+
361+
if len(ret) == 0 || len(ret[0].Rows) == 0 {
362+
return "", nil
363+
}
364+
365+
mac, ok := ret[0].Rows[0]["mac"].(string)
366+
if !ok {
367+
return "", nil
368+
}
369+
return mac, nil
370+
}
371+
348372
func (self *OvsDriver) SetExternalIds(externalIds map[string]string) error {
349373
oMap := buildOVSDBMapFromMap(externalIds)
350374
row := make(map[string]interface{})
@@ -406,8 +430,8 @@ func (self *OvsDriver) UpdateInterface(ifaceName string, externalIDs map[string]
406430

407431
// Create an internal port in OVS
408432
func (self *OvsDriver) CreatePort(intfName, intfType string, vlanTag uint) error {
409-
portUuidStr := intfName
410-
intfUuidStr := fmt.Sprintf("Intf%s", intfName)
433+
portUuidStr := "portdummy"
434+
intfUuidStr := "ifacedummy"
411435
portUuid := []libovsdb.UUID{{GoUuid: portUuidStr}}
412436
intfUuid := []libovsdb.UUID{{GoUuid: intfUuidStr}}
413437
opStr := "insert"

0 commit comments

Comments
 (0)