Skip to content

Commit

Permalink
[Tunnel] Support co-existence of IPv4 and IPv6 tunnels (#147)
Browse files Browse the repository at this point in the history
Support for both IPv4 and IPv6 tunnel for default VRF. IPv6 tunnel must use Vnet-default as Vnet name and IPv4 tunnel must use Vnet-default-v4 as Vnet name.
When system has both V4 and V6 tunnel decap, NON-DEFAULT VRFs always choose V6 tunnel, whereas DEFAULT VRF chose v4 tunnel if VNET name in API is provided as - Vnet-default-v4 and chose v6 tunnel if VNET name is provided as - Vnet-default
When system has only one type of tunnel, all Vnets chose the configured single tunnel (IPv4 or IPv6). No change to existing behavior.
Only one decap tunnel of type V4 and one decap tunnel of type V6 is permitted.
  • Loading branch information
prsunny authored Oct 17, 2023
1 parent c8fa96b commit ccad4a2
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 28 deletions.
64 changes: 53 additions & 11 deletions go-server-server/go/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,22 +901,31 @@ func ConfigTunnelDecapTunnelTypePost(w http.ResponseWriter, r *http.Request) {
return
}

kv, err := GetKVs(db.db_num, generateDBTableKey(db.separator, VXLAN_TUNNEL_TB, "default_vxlan_tunnel"))
tunnel_name := "default_vxlan_tunnel"
// Check if IP address is V4.
if IsValidIP(attr.IPAddr) {
tunnel_name = "default_vxlan_tunnel_v4"
}

kv, err := GetKVs(db.db_num, generateDBTableKey(db.separator, VXLAN_TUNNEL_TB, tunnel_name))
if err != nil {
WriteRequestError(w, http.StatusInternalServerError, "Internal service error", []string{}, "")
return
}

//Check if tunnel already exist and the address family is same
if kv != nil {
WriteRequestErrorWithSubCode(w, http.StatusConflict, RESRC_EXISTS,
"Object already exists: Default Vxlan VTEP", []string{}, "")
return
if isV4orV6(kv["src_ip"]) == isV4orV6(attr.IPAddr) {
WriteRequestErrorWithSubCode(w, http.StatusConflict, RESRC_EXISTS,
"Object already exists: Default Vxlan VTEP", []string{}, "")
return
}
}

pt := swsscommon.NewTable(db.swss_db, VXLAN_TUNNEL_TB)
defer pt.Delete()

pt.Set("default_vxlan_tunnel", map[string]string{
pt.Set(tunnel_name, map[string]string{
"src_ip": attr.IPAddr,
}, "SET", "")

Expand Down Expand Up @@ -1025,18 +1034,36 @@ func ConfigVrouterVrfIdPost(w http.ResponseWriter, r *http.Request) {
return
}

kv, err := GetKVs(db.db_num, generateDBTableKey(db.separator, VXLAN_TUNNEL_TB, "default_vxlan_tunnel"))
tunnel_name := "default_vxlan_tunnel_v4"
kv_4, err := GetKVs(db.db_num, generateDBTableKey(db.separator, VXLAN_TUNNEL_TB, tunnel_name))
if err != nil {
WriteRequestError(w, http.StatusInternalServerError, "Internal service error", []string{}, "")
return
}

if kv == nil {
tunnel_name = "default_vxlan_tunnel"
kv, err := GetKVs(db.db_num, generateDBTableKey(db.separator, VXLAN_TUNNEL_TB, tunnel_name))
if err != nil {
WriteRequestError(w, http.StatusInternalServerError, "Internal service error", []string{}, "")
return
}

if kv == nil && kv_4 == nil {
WriteRequestErrorWithSubCode(w, http.StatusConflict, DEP_MISSING,
"Default VxLAN VTEP must be created prior to creating VRF", []string{"tunnel"}, "")
return
}

var v6_tunnel, v4_tunnel bool
if kv_4 != nil {
tunnel_name = "default_vxlan_tunnel_v4"
v4_tunnel = true
}
if kv != nil {
tunnel_name = "default_vxlan_tunnel"
v6_tunnel = true
}

vnet_id := CacheGetVnetGuidId(vars["vnet_name"])
if vnet_id != 0 {
WriteRequestErrorWithSubCode(w, http.StatusConflict, RESRC_EXISTS,
Expand All @@ -1046,9 +1073,12 @@ func ConfigVrouterVrfIdPost(w http.ResponseWriter, r *http.Request) {

guid := CacheGetVniId(uint32(attr.Vnid))
if guid != "" {
WriteRequestErrorWithSubCode(w, http.StatusConflict, RESRC_EXISTS,
"Object already exists: {\"vni\":\"" + strconv.Itoa(attr.Vnid) + "\", \"vnet_name\":\"" + guid +"\"}", []string{}, "")
return
// Default Vnets can have same Vnid
if !(strings.Contains(guid, "Vnet-default")) {
WriteRequestErrorWithSubCode(w, http.StatusConflict, RESRC_EXISTS,
"Object already exists: {\"vni\":\"" + strconv.Itoa(attr.Vnid) + "\", \"vnet_name\":\"" + guid +"\"}", []string{}, "")
return
}
}

vnet_id = CacheGenAndSetVnetGuidId(vars["vnet_name"], uint32(attr.Vnid))
Expand All @@ -1070,11 +1100,23 @@ func ConfigVrouterVrfIdPost(w http.ResponseWriter, r *http.Request) {

log.Printf("debug: vnet_id_str: "+vnet_id_str)
vnetParams := make(map[string]string)
vnetParams["vxlan_tunnel"] = "default_vxlan_tunnel"
vnetParams["vxlan_tunnel"] = tunnel_name
vnetParams["vni"] = strconv.Itoa(attr.Vnid)
vnetParams["guid"] = vars["vnet_name"]
if strings.Compare(vars["vnet_name"], "Vnet-default") == 0 {
if v6_tunnel == false {
WriteRequestError(w, http.StatusInternalServerError, "Vnet-default is for V6 Tunnels, please create Vnet-default-v4", []string{}, "")
return
}
vnetParams["scope"] = "default"
vnetParams["vxlan_tunnel"] = "default_vxlan_tunnel"
} else if strings.Compare(vars["vnet_name"], "Vnet-default-v4") == 0 {
if v4_tunnel == false {
WriteRequestError(w, http.StatusInternalServerError, "V4 tunnel not created, please create V4 Vxlan Tunnel", []string{}, "")
return
}
vnetParams["scope"] = "default"
vnetParams["vxlan_tunnel"] = "default_vxlan_tunnel_v4"
}
if attr.AdvPrefix != "" {
vnetParams["advertise_prefix"] = attr.AdvPrefix
Expand Down
35 changes: 29 additions & 6 deletions test/apitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ def post_generic_vxlan_tunnel(self):
})
self.assertEqual(rv.status_code, 204)

def post_generic_vxlan_v6_tunnel(self):
rv = self.post_config_tunnel_decap_tunnel_type('vxlan', {
'ip_addr': '2000:1000'
})
self.assertEqual(rv.status_code, 204)

def post_generic_default_vrouter_and_deps(self):
self.post_generic_vxlan_tunnel()
self.post_generic_vxlan_v6_tunnel()
rv = self.post_config_vrouter_vrf_id("vnet-default", {
'vnid': 8000
})
self.assertEqual(rv.status_code, 204)
rv = self.post_config_vrouter_vrf_id("vnet-default-v4", {
'vnid': 8000
})
self.assertEqual(rv.status_code, 204)

def post_generic_vrouter_and_deps(self):
self.post_generic_vxlan_tunnel()
rv = self.post_config_vrouter_vrf_id("vnet-guid-1", {
Expand Down Expand Up @@ -367,7 +385,7 @@ def test_post_config_tunnel_decap_tunnel_type(self):
})
self.assertEqual(r.status_code, 409)

tunnel_table = self.configdb.hgetall(VXLAN_TUNNEL_TB + '|default_vxlan_tunnel')
tunnel_table = self.configdb.hgetall(VXLAN_TUNNEL_TB + '|default_vxlan_tunnel_v4')
self.assertEqual(tunnel_table, {b'src_ip': b'34.53.1.0'})
l.info("Tunnel table is %s", tunnel_table)

Expand All @@ -376,7 +394,7 @@ def test_delete_config_tunnel_decap_tunnel_type(self):
r = self.delete_config_tunnel_decap_tunnel_type('vxlan')
self.assertEqual(r.status_code, 204)
# The delete is a no-op and should return 204, moreover the tunnel should not be deleted
tunnel_table = self.configdb.hgetall(VXLAN_TUNNEL_TB + '|default_vxlan_tunnel')
tunnel_table = self.configdb.hgetall(VXLAN_TUNNEL_TB + '|default_vxlan_tunnel_v4')
self.assertEqual(tunnel_table, {b'src_ip': b'34.53.1.0'})


Expand Down Expand Up @@ -406,7 +424,7 @@ def test_post_vrouter(self):

vrouter_table = self.configdb.hgetall(VNET_TB + '|' + VNET_NAME_PREF + '1')
self.assertEqual(vrouter_table, {
b'vxlan_tunnel': b'default_vxlan_tunnel',
b'vxlan_tunnel': b'default_vxlan_tunnel_v4',
b'vni': b'1001',
b'guid': b'vnet-guid-1'
})
Expand All @@ -415,6 +433,11 @@ def test_get_vrouter(self):
self.post_generic_vrouter_and_deps()
self.check_vrouter_exists("vnet-guid-1",1001)

def test_default_vrouter(self):
self.post_generic_default_vrouter_and_deps()
self.check_vrouter_exists("vnet-default",8000)
self.check_vrouter_exists("vnet-default-v4",8000)

def test_duplicate_vni(self):
self.post_generic_vrouter_and_deps_duplicate()
self.check_vrouter_exists("vnet-guid-1",1001)
Expand Down Expand Up @@ -447,7 +470,7 @@ def test_vnet_name_mapping_logic(self):
self.check_vrouter_exists("vnet-guid-"+str(i), 1000+i)
vrouter_table = self.configdb.hgetall(VNET_TB + '|' + VNET_NAME_PREF +str(i))
self.assertEqual(vrouter_table, {
b'vxlan_tunnel': b'default_vxlan_tunnel',
b'vxlan_tunnel': b'default_vxlan_tunnel_v4',
b'vni': b'100'+str(i),
b'guid': b'vnet-guid-'+str(i)
})
Expand All @@ -460,7 +483,7 @@ def test_vnet_name_mapping_logic(self):
self.check_vrouter_exists("vnet-guid-"+str(i+3), 1003+i)
vrouter_table = self.configdb.hgetall(VNET_TB + '|' + VNET_NAME_PREF +str(i))
self.assertEqual(vrouter_table, {
b'vxlan_tunnel': b'default_vxlan_tunnel',
b'vxlan_tunnel': b'default_vxlan_tunnel_v4',
b'vni': b'100'+str(i+3),
b'guid': b'vnet-guid-'+str(i+3)
})
Expand All @@ -470,7 +493,7 @@ def test_vnet_name_mapping_logic(self):
self.check_vrouter_exists("vnet-guid-"+str(i+6), 1006+i)
vrouter_table = self.configdb.hgetall(VNET_TB + '|' + VNET_NAME_PREF +str(i+3))
self.assertEqual(vrouter_table, {
b'vxlan_tunnel': b'default_vxlan_tunnel',
b'vxlan_tunnel': b'default_vxlan_tunnel_v4',
b'vni': b'100'+str(i+6),
b'guid': b'vnet-guid-'+str(i+6)
})
Expand Down
19 changes: 19 additions & 0 deletions test/restapi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ def post_generic_vxlan_tunnel(self):
})
assert rv.status_code == 204

def post_generic_vxlan_v6_tunnel(self):
rv = self.post_config_tunnel_decap_tunnel_type('vxlan', {
'ip_addr': '2000::1000'
})
assert rv.status_code == 204

def post_generic_default_vrouter_and_deps(self):
self.post_generic_vxlan_tunnel()
self.post_generic_vxlan_v6_tunnel()
rv = self.post_config_vrouter_vrf_id("Vnet-default", {
'vnid': 8000
})
assert rv.status_code == 204

rv = self.post_config_vrouter_vrf_id("Vnet-default-v4", {
'vnid': 8000
})
assert rv.status_code == 204

def post_generic_vrouter_and_deps(self):
self.post_generic_vxlan_tunnel()
rv = self.post_config_vrouter_vrf_id("vnet-guid-1", {
Expand Down
Loading

0 comments on commit ccad4a2

Please sign in to comment.