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

« back to all changes in this revision

Viewing changes to net/vnetwork.c

  • Committer: Dmitrii Zagorodnov
  • Date: 2009-01-27 21:53:41 UTC
  • mfrom: (25.1.112 eucalyptus-main)
  • Revision ID: dmitrii@cs.ucsb.edu-20090127215341-i0f0v6cmbpljmg02
merged with current main

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
        snprintf(cmd, 256, "-A POSTROUTING -d ! %s/%d -j MASQUERADE", network, slashnet);
80
80
        rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
81
81
 
 
82
        snprintf(cmd, 256, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add 169.254.169.254 dev %s", vnetconfig->eucahome, vnetconfig->pubInterface);
 
83
        rc = system(cmd);
 
84
        
 
85
        snprintf(cmd, 256, "-A PREROUTING -s %s/%d -d 169.254.169.254 -p tcp --dport 80 -j DNAT --to 169.254.169.254:8773", network, slashnet);
 
86
        rc = vnetApplySingleTableRule(vnetconfig, "nat", cmd);
 
87
 
82
88
        unm = 0xFFFFFFFF - numaddrs;
83
89
        unw = nw;
84
90
        for (vlan=2; vlan<NUMBER_OF_VLANS; vlan++) {
637
643
}
638
644
 
639
645
int check_device(char *dev) {
640
 
  char rbuf[256], devbuf[256];
 
646
  char rbuf[256], devbuf[256], *ptr;
641
647
  FILE *FH=NULL;
642
648
  
 
649
  
643
650
  if (!dev) return(1);
644
 
 
645
 
  snprintf(devbuf, 255, "%s:", dev);
646
 
 
 
651
  
647
652
  FH = fopen("/proc/net/dev", "r");
648
653
  if (!FH) {
649
654
    return(1);
650
655
  }
651
656
  
652
657
  while(fgets(rbuf, 256, FH)) {
653
 
    if (strstr(rbuf, devbuf)) {
654
 
      fclose(FH);
655
 
      return(0);
 
658
    ptr = strrchr(rbuf, ':');
 
659
    if (ptr) {
 
660
      *ptr = '\0';
 
661
      ptr = strrchr(rbuf, ' ');
 
662
      if (ptr) {
 
663
        ptr = ptr + 1;
 
664
      } else {
 
665
        ptr = rbuf;
 
666
      }
 
667
      if (!strcmp(ptr, dev)) {
 
668
        // found it
 
669
        fclose(FH);
 
670
        return(0);
 
671
      }
656
672
    }
657
673
  }
658
674
  fclose(FH);
661
677
}
662
678
 
663
679
int check_bridge(char *brname) {
664
 
  char rbuf[256], devbuf[256];
665
 
  FILE *FH=NULL;
666
 
  
667
 
  if (!brname) return(1);
668
 
 
669
 
  snprintf(devbuf, 255, "%s:", brname);
670
 
 
671
 
  FH = fopen("/proc/net/dev", "r");
672
 
  if (!FH) {
673
 
    return(1);
674
 
  }
675
 
  
676
 
  while(fgets(rbuf, 256, FH)) {
677
 
    if (strstr(rbuf, devbuf)) {
678
 
      fclose(FH);
679
 
      return(0);
680
 
    }
681
 
  }
682
 
  fclose(FH);
683
 
 
684
 
  return(1);
 
680
  return(check_device(brname));
685
681
}
686
682
 
687
683
int vnetStartNetworkManaged(vnetConfig *vnetconfig, int vlan, char *userName, char *netName, char **outbrname) {
730
726
        }      
731
727
      }
732
728
      
 
729
      // add if to bridge
733
730
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap brctl addif %s %s", vnetconfig->eucahome, newbrname, newdevname);
734
731
      rc = system(cmd);
735
732
      
 
733
      // bring br up
 
734
      //      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newbrname);
 
735
      //      rc = system(cmd);
 
736
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newbrname);
 
737
      rc = system(cmd);
 
738
      
 
739
      // bring if up
 
740
      //      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newdevname);
 
741
      //      rc = system(cmd);
 
742
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newdevname);
 
743
      rc = system(cmd);
 
744
 
 
745
      /*
736
746
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s 0.0.0.0 up", vnetconfig->eucahome, newbrname);
737
747
      rc = system(cmd);
738
748
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s up", vnetconfig->eucahome, newdevname);
739
749
      rc = system(cmd);
 
750
      */
740
751
      
741
752
    } else {
742
753
      snprintf(newbrname, 32, "%s", vnetconfig->bridgedev);
760
771
          return(1);
761
772
        }
762
773
      }
 
774
 
 
775
      // create new bridge                                                                                                                                     
 
776
      snprintf(newbrname, 32, "eucabr%d", vlan);
 
777
      rc = check_bridge(newbrname);
 
778
      if (rc) {
 
779
        // bridge does not yet exist
 
780
        snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap brctl addbr %s", vnetconfig->eucahome, newbrname);
 
781
        rc = system(cmd);
 
782
        if (rc) {
 
783
          logprintfl(EUCAERROR, "could not create new bridge %s\n", newbrname);
 
784
          return(1);
 
785
        }
 
786
      }
 
787
 
 
788
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap brctl addif %s %s", vnetconfig->eucahome, newbrname, newdevname);
 
789
      rc = system(cmd);
 
790
      
 
791
      // bring br up
 
792
      //      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newbrname);
 
793
      //      rc = system(cmd);
 
794
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newbrname);
 
795
      rc = system(cmd);
 
796
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip addr flush %s", vnetconfig->eucahome, newbrname);
 
797
      rc = system(cmd);
 
798
 
 
799
      // bring if up
 
800
      //      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add 0.0.0.0 dev %s", vnetconfig->eucahome, newdevname);
 
801
      //      rc = system(cmd);
 
802
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, newdevname);
 
803
      rc = system(cmd);
 
804
 
 
805
      snprintf(newdevname, 32, "%s", newbrname);
763
806
    } else {
764
807
      snprintf(newdevname, 32, "%s", vnetconfig->pubInterface);
765
808
    }
768
811
    if (rc) {
769
812
      return(rc);
770
813
    }
771
 
    /*
772
 
      newip = hex2dot(vnetconfig->networks[vlan].router);
773
 
      netmask = hex2dot(vnetconfig->networks[vlan].nm);
774
 
      
775
 
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s %s netmask %s up", vnetconfig->eucahome, newdevname, newip, netmask);
776
 
      rc = system(cmd);
777
 
      if (rc) {
778
 
      logprintfl(EUCAERROR, "could not bring up new device %s with ip %s\n", newdevname, newip);
779
 
      if (newip) free(newip);
780
 
      if (netmask) free(netmask);
781
 
      return(1);
782
 
      }
783
 
      if (newip) free(newip);
784
 
      if (netmask) free(netmask);
785
 
    */
786
814
    
787
815
    *outbrname = strdup(newdevname);
788
816
  }
812
840
  if (newip) free(newip);
813
841
  if (broadcast) free(broadcast);
814
842
 
815
 
  snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s up", vnetconfig->eucahome, devname);
 
843
  snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s up", vnetconfig->eucahome, devname);
816
844
  rc = system(cmd);
817
845
  if (rc) {
818
846
    logprintfl(EUCAERROR, "could not bring up interface '%s'\n", devname);
851
879
  int rc, ret;
852
880
  
853
881
  ret = 0;
854
 
  if (vnetconfig->role == NC) {
 
882
  //if (vnetconfig->role == NC) {
855
883
    
856
 
    if (!strcmp(vnetconfig->mode, "MANAGED")) {
857
 
      snprintf(newbrname, 32, "eucabr%d", vlan);
858
 
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s down", vnetconfig->eucahome, newbrname);
859
 
      rc = system(cmd);
860
 
      if (rc) {
861
 
        logprintfl(EUCAERROR, "cmd '%s' failed\n", cmd);
862
 
        ret = 1;
863
 
      }
 
884
  if (!strcmp(vnetconfig->mode, "MANAGED")) {
 
885
    snprintf(newbrname, 32, "eucabr%d", vlan);
 
886
    snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s down", vnetconfig->eucahome, newbrname);
 
887
    rc = system(cmd);
 
888
    if (rc) {
 
889
      logprintfl(EUCAERROR, "cmd '%s' failed\n", cmd);
 
890
      ret = 1;
864
891
    }
 
892
  }
865
893
 
866
 
  }
 
894
    //  }
867
895
  
868
896
  if (!strcmp(vnetconfig->mode, "MANAGED")) {
869
897
    snprintf(newdevname, 32, "%s.%d", vnetconfig->pubInterface, vlan);
870
898
    rc = check_device(newdevname);
871
899
    if (!rc) {
872
 
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ifconfig %s down", vnetconfig->eucahome, newdevname);
 
900
      snprintf(cmd, 1024, "%s/usr/share/eucalyptus/euca_rootwrap ip link set dev %s down", vnetconfig->eucahome, newdevname);
873
901
      rc = system(cmd);
874
902
      if (rc) {
875
903
        logprintfl(EUCAERROR, "cmd '%s' failed\n", cmd);
884
912
        ret = 1;
885
913
      }
886
914
    }
 
915
    snprintf(newdevname, 32, "%s", newbrname);
887
916
  } else {
888
917
    snprintf(newdevname, 32, "%s", vnetconfig->pubInterface);
889
918
  }
895
924
      if (rc) {
896
925
        logprintfl(EUCAERROR, "could not remove '%s' from list of interfaces\n", newdevname);
897
926
      }
898
 
    } else {
899
 
      rc = vnetDelGatewayIP(vnetconfig, vlan, newdevname);
900
 
    }
 
927
    } 
 
928
    rc = vnetDelGatewayIP(vnetconfig, vlan, newdevname);
901
929
    
902
930
    if (userName && netName) {
903
931
      rc = vnetDeleteChain(vnetconfig, userName, netName);