~ubuntu-core-dev/eucalyptus/ubuntu-karmic

« back to all changes in this revision

Viewing changes to net/vnetwork.c

  • Committer: root
  • Date: 2009-08-01 20:59:22 UTC
  • Revision ID: root@sg-20090801205922-2joxywoc7p9jl2mh
fixed build, added vtun tunneling code

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <vnetwork.h>
21
21
#include <misc.h>
22
22
 
23
 
void vnetInit(vnetConfig *vnetconfig, char *mode, char *eucahome, char *path, int role, char *pubInterface, char *privInterface, char *numberofaddrs, char *network, char *netmask, char *broadcast, char *nameserver, char *router, char *daemon, char *dhcpuser, char *bridgedev) {
 
23
void vnetInit(vnetConfig *vnetconfig, char *mode, char *eucahome, char *path, int role, char *pubInterface, char *privInterface, char *numberofaddrs, char *network, char *netmask, char *broadcast, char *nameserver, char *router, char *daemon, char *dhcpuser, char *bridgedev, char *localIp) {
24
24
  uint32_t nw=0, nm=0, unw=0, unm=0, dns=0, bc=0, rt=0, rc=0, slashnet=0;
25
25
  int vlan=0, numaddrs=1;
26
26
  char cmd[256];
37
37
    if (bridgedev) strncpy(vnetconfig->bridgedev, bridgedev, 32);
38
38
    if (daemon) strncpy(vnetconfig->dhcpdaemon, daemon, 1024);
39
39
    if (dhcpuser) strncpy(vnetconfig->dhcpuser, dhcpuser, 32);
 
40
    if (localIp) strncpy(vnetconfig->localIp, localIp, 32);
40
41
    vnetconfig->role = role;
41
42
    vnetconfig->enabled=1;
42
43
    vnetconfig->initialized = 1;
824
825
  char rbuf[256], devbuf[256], *ptr;
825
826
  FILE *FH=NULL;
826
827
  
827
 
  
828
828
  if (!dev) return(1);
829
829
  
830
830
  FH = fopen("/proc/net/dev", "r");
850
850
    }
851
851
  }
852
852
  fclose(FH);
853
 
 
 
853
  
854
854
  return(1);
855
855
}
856
856
 
858
858
  return(check_device(brname));
859
859
}
860
860
 
 
861
int vnetSetCCS(vnetConfig *vnetconfig, char **ccs, int ccsLen) {
 
862
  int i;
 
863
  
 
864
  if (ccsLen > NUMBER_OF_CCS) {
 
865
    logprintfl(EUCAERROR, "specified number of cluster controllers exceeds max '%d'\n", NUMBER_OF_CCS);
 
866
    return(1);
 
867
  }
 
868
 
 
869
  if (ccsLen > 0) {
 
870
    bzero(vnetconfig->ccs, sizeof(uint32_t) * NUMBER_OF_CCS);
 
871
    for (i=0; i<ccsLen; i++) {
 
872
      vnetconfig->ccs[i] = dot2hex(ccs[i]);
 
873
    }
 
874
  }
 
875
  return(0);
 
876
}
 
877
 
861
878
int vnetStartNetworkManaged(vnetConfig *vnetconfig, int vlan, char *userName, char *netName, char **outbrname) {
862
879
  char cmd[1024], newdevname[32], newbrname[32];
863
880
  int rc;
880
897
  }
881
898
 
882
899
  if (vnetconfig->role == NC && vlan > 0) {
883
 
 
884
900
    // first, create tagged interface
885
901
    if (!strcmp(vnetconfig->mode, "MANAGED")) {
886
902
      snprintf(newdevname, 32, "%s.%d", vnetconfig->privInterface, vlan);
914
930
      rc = system(cmd);
915
931
      
916
932
      // bring br up
917
 
      //      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newbrname);
918
 
      //      rc = system(cmd);
919
933
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newbrname);
920
934
      rc = system(cmd);
921
935
      
922
936
      // bring if up
923
 
      //      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newdevname);
924
 
      //      rc = system(cmd);
925
937
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newdevname);
926
938
      rc = system(cmd);
927
 
 
928
 
      /*
929
 
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ifconfig %s 0.0.0.0 up", vnetconfig->eucahome, newbrname);
930
 
      rc = system(cmd);
931
 
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ifconfig %s up", vnetconfig->eucahome, newdevname);
932
 
      rc = system(cmd);
933
 
      */
934
 
      
935
939
    } else {
936
940
      snprintf(newbrname, 32, "%s", vnetconfig->bridgedev);
937
941
    }
956
960
        }
957
961
      }
958
962
 
959
 
      // create new bridge                                                                                                                                     
 
963
      // create new bridge
960
964
      snprintf(newbrname, 32, "eucabr%d", vlan);
961
965
      rc = check_bridge(newbrname);
962
966
      if (rc) {
968
972
          return(1);
969
973
        }
970
974
      }
971
 
 
 
975
      
972
976
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap brctl addif %s %s", vnetconfig->eucahome, newbrname, newdevname);
973
977
      rc = system(cmd);
974
978
      
975
979
      // bring br up
976
 
      //      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newbrname);
977
 
      //      rc = system(cmd);
978
980
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newbrname);
979
981
      rc = system(cmd);
980
982
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip addr flush %s", vnetconfig->eucahome, newbrname);
981
983
      rc = system(cmd);
982
 
 
 
984
      
983
985
      // bring if up
984
 
      //      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newdevname);
985
 
      //      rc = system(cmd);
986
986
      snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newdevname);
987
987
      rc = system(cmd);
988
988
 
1001
1001
  return(0);
1002
1002
}
1003
1003
 
 
1004
int vnetTeardownTunnels(vnetConfig *vnetconfig) {
 
1005
  int i, done, j, rc;
 
1006
  
 
1007
  for (j=2; j<NUMBER_OF_VLANS; j++) {
 
1008
    if (vnetconfig->networks[j].active) {
 
1009
      done=0;
 
1010
      for (i=0; i<NUMBER_OF_CCS && !done; i++) {
 
1011
        if (vnetconfig->ccs[i] == 0) {
 
1012
          done++;
 
1013
        } else {
 
1014
          char cmd[1024], gredev[32];
 
1015
          
 
1016
          snprintf(gredev, 32, "gretun%d.%d", i, j);
 
1017
          
 
1018
          logprintfl(EUCADEBUG, "tearing down tunnel: %s\n", gredev);
 
1019
          rc = check_device(gredev);
 
1020
          if (!rc) {
 
1021
            snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link del dev %s", vnetconfig->eucahome, gredev);
 
1022
            logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1023
            rc = system(cmd);
 
1024
            rc = rc>>8;
 
1025
            logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1026
          }
 
1027
        }
 
1028
      }
 
1029
    }
 
1030
  }
 
1031
  return(0);
 
1032
}
 
1033
int vnetSetupTunnelsVTUN(vnetConfig *vnetconfig) {
 
1034
  return(vnetSetupTunnelsVTUN(vnetconfig));
 
1035
}
 
1036
int vnetSetupTunnelsVTUN(vnetConfig *vnetconfig) {
 
1037
  int i, done, j, rc;
 
1038
  
 
1039
  snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap vtund -s -f %s/var/lib/CC/vtunall.conf", vnetconfig->eucahome, vnetconfig->eucahome);
 
1040
  logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1041
  rc = system(cmd);
 
1042
  rc = rc>>8;
 
1043
  logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1044
 
 
1045
  for (j=2; j<NUMBER_OF_VLANS; j++) {
 
1046
    char brdev[32];
 
1047
    snprintf(brdev, 32, "eucabr%d", j);
 
1048
    
 
1049
    if (vnetconfig->networks[j].active && !check_device(brdev)) {
 
1050
      done=0;
 
1051
      for (i=0; i<NUMBER_OF_CCS && !done; i++) {
 
1052
        if (vnetconfig->ccs[i] == 0) {
 
1053
          done++;
 
1054
        } else {
 
1055
          char cmd[1024], tundev[32], *remoteIp=NULL;
 
1056
          remoteIp = hex2dot(vnetconfig->ccs[i]);
 
1057
          
 
1058
          if (strcmp(remoteIp, vnetconfig->localIp)) {
 
1059
            logprintfl(EUCADEBUG, "setting up tunnel for endpoint: %s\n", remoteIp);
 
1060
            snprintf(tundev, 32, "vtun%d.%d", i, j);
 
1061
            rc = check_device(tundev);
 
1062
            if (rc) {
 
1063
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap vtund -f %s/var/lib/CC/vtunall.conf -p tun-%d-%d %s", vnetconfig->eucahome, vnetconfig->eucahome, i, j, remoteIp);
 
1064
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1065
              rc = system(cmd);
 
1066
              rc = rc>>8;
 
1067
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1068
 
 
1069
              //vtund -f /tmp/vtun.lespaul -p tun10 sg
 
1070
              /*
 
1071
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link add %s type gretap remote %s local %s ttl 15", vnetconfig->eucahome, tundev, remoteIp, vnetconfig->localIp);
 
1072
              */
 
1073
              
 
1074
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap brctl addif %s %s", vnetconfig->eucahome, brdev, tundev);
 
1075
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1076
              rc = system(cmd);
 
1077
              rc = rc>>8;
 
1078
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1079
              
 
1080
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set up dev %s", vnetconfig->eucahome, tundev);
 
1081
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1082
              rc = system(cmd);
 
1083
              rc = rc>>8;
 
1084
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1085
              
 
1086
              if (remoteIp) free(remoteIp);
 
1087
            }
 
1088
          }
 
1089
        }
 
1090
      }
 
1091
    }
 
1092
  }  
 
1093
  
 
1094
  return(0);
 
1095
}
 
1096
int vnetSetupTunnelsGRE(vnetConfig *vnetconfig) {
 
1097
  int i, done, j, rc;
 
1098
  
 
1099
  for (j=2; j<NUMBER_OF_VLANS; j++) {
 
1100
    char brdev[32];
 
1101
    snprintf(brdev, 32, "eucabr%d", j);
 
1102
    
 
1103
    if (vnetconfig->networks[j].active && !check_device(brdev)) {
 
1104
      done=0;
 
1105
      for (i=0; i<NUMBER_OF_CCS && !done; i++) {
 
1106
        if (vnetconfig->ccs[i] == 0) {
 
1107
          done++;
 
1108
        } else {
 
1109
          char cmd[1024], tundev[32], *remoteIp=NULL;
 
1110
          remoteIp = hex2dot(vnetconfig->ccs[i]);
 
1111
          
 
1112
          if (strcmp(remoteIp, vnetconfig->localIp)) {
 
1113
            logprintfl(EUCADEBUG, "setting up tunnel for endpoint: %s\n", remoteIp);
 
1114
            snprintf(tundev, 32, "gretun%d.%d", i, j);
 
1115
            rc = check_device(tundev);
 
1116
            if (rc) {
 
1117
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link add %s type gretap remote %s local %s ttl 15", vnetconfig->eucahome, tundev, remoteIp, vnetconfig->localIp);
 
1118
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1119
              rc = system(cmd);
 
1120
              rc = rc>>8;
 
1121
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1122
              
 
1123
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap brctl addif %s %s", vnetconfig->eucahome, brdev, tundev);
 
1124
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1125
              rc = system(cmd);
 
1126
              rc = rc>>8;
 
1127
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1128
              
 
1129
              snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap ip link set up dev %s", vnetconfig->eucahome, tundev);
 
1130
              logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
1131
              rc = system(cmd);
 
1132
              rc = rc>>8;
 
1133
              logprintfl(EUCADEBUG, "done: %d\n", rc);
 
1134
              
 
1135
              if (remoteIp) free(remoteIp);
 
1136
            }
 
1137
          }
 
1138
        }
 
1139
      }
 
1140
    }
 
1141
  }
 
1142
  return(0);
 
1143
}
 
1144
 
1004
1145
int vnetAddGatewayIP(vnetConfig *vnetconfig, int vlan, char *devname) {
1005
1146
  char *newip, *broadcast;
1006
1147
  int rc, slashnet;
1069
1210
    logprintfl(EUCAWARN, "supplied vlan '%d' is out of range (%d - %d), nothing to do\n", vlan, 0, vnetconfig->max_vlan);
1070
1211
    return(0);
1071
1212
  }
1072
 
 
 
1213
  
 
1214
  rc = vnetTeardownTunnels(vnetconfig);
 
1215
  
1073
1216
  vnetconfig->networks[vlan].active = 0;
1074
1217
 
1075
1218
  if (!strcmp(vnetconfig->mode, "MANAGED")) {