datapath.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  58. static void rehash_flow_table(struct work_struct *work);
  59. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  60. int ovs_net_id __read_mostly;
  61. static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
  62. struct genl_multicast_group *grp)
  63. {
  64. genl_notify(skb, genl_info_net(info), info->snd_portid,
  65. grp->id, info->nlhdr, GFP_KERNEL);
  66. }
  67. /**
  68. * DOC: Locking:
  69. *
  70. * All writes e.g. Writes to device state (add/remove datapath, port, set
  71. * operations on vports, etc.), Writes to other state (flow table
  72. * modifications, set miscellaneous datapath parameters, etc.) are protected
  73. * by ovs_lock.
  74. *
  75. * Reads are protected by RCU.
  76. *
  77. * There are a few special cases (mostly stats) that have their own
  78. * synchronization but they nest under all of above and don't interact with
  79. * each other.
  80. *
  81. * The RTNL lock nests inside ovs_mutex.
  82. */
  83. static DEFINE_MUTEX(ovs_mutex);
  84. void ovs_lock(void)
  85. {
  86. mutex_lock(&ovs_mutex);
  87. }
  88. void ovs_unlock(void)
  89. {
  90. mutex_unlock(&ovs_mutex);
  91. }
  92. #ifdef CONFIG_LOCKDEP
  93. int lockdep_ovsl_is_held(void)
  94. {
  95. if (debug_locks)
  96. return lockdep_is_held(&ovs_mutex);
  97. else
  98. return 1;
  99. }
  100. #endif
  101. static struct vport *new_vport(const struct vport_parms *);
  102. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  103. const struct dp_upcall_info *);
  104. static int queue_userspace_packet(struct net *, int dp_ifindex,
  105. struct sk_buff *,
  106. const struct dp_upcall_info *);
  107. /* Must be called with rcu_read_lock or ovs_mutex. */
  108. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  109. {
  110. struct datapath *dp = NULL;
  111. struct net_device *dev;
  112. rcu_read_lock();
  113. dev = dev_get_by_index_rcu(net, dp_ifindex);
  114. if (dev) {
  115. struct vport *vport = ovs_internal_dev_get_vport(dev);
  116. if (vport)
  117. dp = vport->dp;
  118. }
  119. rcu_read_unlock();
  120. return dp;
  121. }
  122. /* Must be called with rcu_read_lock or ovs_mutex. */
  123. const char *ovs_dp_name(const struct datapath *dp)
  124. {
  125. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  126. return vport->ops->get_name(vport);
  127. }
  128. static int get_dpifindex(struct datapath *dp)
  129. {
  130. struct vport *local;
  131. int ifindex;
  132. rcu_read_lock();
  133. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  134. if (local)
  135. ifindex = netdev_vport_priv(local)->dev->ifindex;
  136. else
  137. ifindex = 0;
  138. rcu_read_unlock();
  139. return ifindex;
  140. }
  141. static void destroy_dp_rcu(struct rcu_head *rcu)
  142. {
  143. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  144. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  145. free_percpu(dp->stats_percpu);
  146. release_net(ovs_dp_get_net(dp));
  147. kfree(dp->ports);
  148. kfree(dp);
  149. }
  150. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  151. u16 port_no)
  152. {
  153. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  154. }
  155. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  156. {
  157. struct vport *vport;
  158. struct hlist_head *head;
  159. head = vport_hash_bucket(dp, port_no);
  160. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  161. if (vport->port_no == port_no)
  162. return vport;
  163. }
  164. return NULL;
  165. }
  166. /* Called with ovs_mutex. */
  167. static struct vport *new_vport(const struct vport_parms *parms)
  168. {
  169. struct vport *vport;
  170. vport = ovs_vport_add(parms);
  171. if (!IS_ERR(vport)) {
  172. struct datapath *dp = parms->dp;
  173. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  174. hlist_add_head_rcu(&vport->dp_hash_node, head);
  175. }
  176. return vport;
  177. }
  178. void ovs_dp_detach_port(struct vport *p)
  179. {
  180. ASSERT_OVSL();
  181. /* First drop references to device. */
  182. hlist_del_rcu(&p->dp_hash_node);
  183. /* Then destroy it. */
  184. ovs_vport_del(p);
  185. }
  186. /* Must be called with rcu_read_lock. */
  187. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  188. {
  189. struct datapath *dp = p->dp;
  190. struct sw_flow *flow;
  191. struct dp_stats_percpu *stats;
  192. struct sw_flow_key key;
  193. u64 *stats_counter;
  194. int error;
  195. int key_len;
  196. stats = this_cpu_ptr(dp->stats_percpu);
  197. /* Extract flow from 'skb' into 'key'. */
  198. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  199. if (unlikely(error)) {
  200. kfree_skb(skb);
  201. return;
  202. }
  203. /* Look up flow. */
  204. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  205. if (unlikely(!flow)) {
  206. struct dp_upcall_info upcall;
  207. upcall.cmd = OVS_PACKET_CMD_MISS;
  208. upcall.key = &key;
  209. upcall.userdata = NULL;
  210. upcall.portid = p->upcall_portid;
  211. ovs_dp_upcall(dp, skb, &upcall);
  212. consume_skb(skb);
  213. stats_counter = &stats->n_missed;
  214. goto out;
  215. }
  216. OVS_CB(skb)->flow = flow;
  217. stats_counter = &stats->n_hit;
  218. ovs_flow_used(OVS_CB(skb)->flow, skb);
  219. ovs_execute_actions(dp, skb);
  220. out:
  221. /* Update datapath statistics. */
  222. u64_stats_update_begin(&stats->sync);
  223. (*stats_counter)++;
  224. u64_stats_update_end(&stats->sync);
  225. }
  226. static struct genl_family dp_packet_genl_family = {
  227. .id = GENL_ID_GENERATE,
  228. .hdrsize = sizeof(struct ovs_header),
  229. .name = OVS_PACKET_FAMILY,
  230. .version = OVS_PACKET_VERSION,
  231. .maxattr = OVS_PACKET_ATTR_MAX,
  232. .netnsok = true,
  233. .parallel_ops = true,
  234. };
  235. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  236. const struct dp_upcall_info *upcall_info)
  237. {
  238. struct dp_stats_percpu *stats;
  239. int dp_ifindex;
  240. int err;
  241. if (upcall_info->portid == 0) {
  242. err = -ENOTCONN;
  243. goto err;
  244. }
  245. dp_ifindex = get_dpifindex(dp);
  246. if (!dp_ifindex) {
  247. err = -ENODEV;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->sync);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->sync);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct net *net, int dp_ifindex,
  265. struct sk_buff *skb,
  266. const struct dp_upcall_info *upcall_info)
  267. {
  268. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  269. struct dp_upcall_info later_info;
  270. struct sw_flow_key later_key;
  271. struct sk_buff *segs, *nskb;
  272. int err;
  273. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  274. if (IS_ERR(segs))
  275. return PTR_ERR(segs);
  276. /* Queue all of the segments. */
  277. skb = segs;
  278. do {
  279. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  280. if (err)
  281. break;
  282. if (skb == segs && gso_type & SKB_GSO_UDP) {
  283. /* The initial flow key extracted by ovs_flow_extract()
  284. * in this case is for a first fragment, so we need to
  285. * properly mark later fragments.
  286. */
  287. later_key = *upcall_info->key;
  288. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  289. later_info = *upcall_info;
  290. later_info.key = &later_key;
  291. upcall_info = &later_info;
  292. }
  293. } while ((skb = skb->next));
  294. /* Free all of the segments. */
  295. skb = segs;
  296. do {
  297. nskb = skb->next;
  298. if (err)
  299. kfree_skb(skb);
  300. else
  301. consume_skb(skb);
  302. } while ((skb = nskb));
  303. return err;
  304. }
  305. static size_t key_attr_size(void)
  306. {
  307. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  308. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  309. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  310. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  311. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  312. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  313. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  314. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  315. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  316. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  317. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  318. }
  319. static size_t upcall_msg_size(const struct sk_buff *skb,
  320. const struct nlattr *userdata)
  321. {
  322. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  323. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  324. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  325. /* OVS_PACKET_ATTR_USERDATA */
  326. if (userdata)
  327. size += NLA_ALIGN(userdata->nla_len);
  328. return size;
  329. }
  330. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  331. struct sk_buff *skb,
  332. const struct dp_upcall_info *upcall_info)
  333. {
  334. struct ovs_header *upcall;
  335. struct sk_buff *nskb = NULL;
  336. struct sk_buff *user_skb; /* to be queued to userspace */
  337. struct nlattr *nla;
  338. int err;
  339. if (vlan_tx_tag_present(skb)) {
  340. nskb = skb_clone(skb, GFP_ATOMIC);
  341. if (!nskb)
  342. return -ENOMEM;
  343. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  344. if (!nskb)
  345. return -ENOMEM;
  346. nskb->vlan_tci = 0;
  347. skb = nskb;
  348. }
  349. if (nla_attr_size(skb->len) > USHRT_MAX) {
  350. err = -EFBIG;
  351. goto out;
  352. }
  353. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  354. if (!user_skb) {
  355. err = -ENOMEM;
  356. goto out;
  357. }
  358. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  359. 0, upcall_info->cmd);
  360. upcall->dp_ifindex = dp_ifindex;
  361. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  362. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  363. nla_nest_end(user_skb, nla);
  364. if (upcall_info->userdata)
  365. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  366. nla_len(upcall_info->userdata),
  367. nla_data(upcall_info->userdata));
  368. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  369. skb_copy_and_csum_dev(skb, nla_data(nla));
  370. genlmsg_end(user_skb, upcall);
  371. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  372. out:
  373. kfree_skb(nskb);
  374. return err;
  375. }
  376. /* Called with ovs_mutex. */
  377. static int flush_flows(struct datapath *dp)
  378. {
  379. struct flow_table *old_table;
  380. struct flow_table *new_table;
  381. old_table = ovsl_dereference(dp->table);
  382. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  383. if (!new_table)
  384. return -ENOMEM;
  385. rcu_assign_pointer(dp->table, new_table);
  386. ovs_flow_tbl_deferred_destroy(old_table);
  387. return 0;
  388. }
  389. static int validate_actions(const struct nlattr *attr,
  390. const struct sw_flow_key *key, int depth);
  391. static int validate_sample(const struct nlattr *attr,
  392. const struct sw_flow_key *key, int depth)
  393. {
  394. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  395. const struct nlattr *probability, *actions;
  396. const struct nlattr *a;
  397. int rem;
  398. memset(attrs, 0, sizeof(attrs));
  399. nla_for_each_nested(a, attr, rem) {
  400. int type = nla_type(a);
  401. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  402. return -EINVAL;
  403. attrs[type] = a;
  404. }
  405. if (rem)
  406. return -EINVAL;
  407. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  408. if (!probability || nla_len(probability) != sizeof(u32))
  409. return -EINVAL;
  410. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  411. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  412. return -EINVAL;
  413. return validate_actions(actions, key, depth + 1);
  414. }
  415. static int validate_tp_port(const struct sw_flow_key *flow_key)
  416. {
  417. if (flow_key->eth.type == htons(ETH_P_IP)) {
  418. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  419. return 0;
  420. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  421. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  422. return 0;
  423. }
  424. return -EINVAL;
  425. }
  426. static int validate_set(const struct nlattr *a,
  427. const struct sw_flow_key *flow_key)
  428. {
  429. const struct nlattr *ovs_key = nla_data(a);
  430. int key_type = nla_type(ovs_key);
  431. /* There can be only one key in a action */
  432. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  433. return -EINVAL;
  434. if (key_type > OVS_KEY_ATTR_MAX ||
  435. nla_len(ovs_key) != ovs_key_lens[key_type])
  436. return -EINVAL;
  437. switch (key_type) {
  438. const struct ovs_key_ipv4 *ipv4_key;
  439. const struct ovs_key_ipv6 *ipv6_key;
  440. case OVS_KEY_ATTR_PRIORITY:
  441. case OVS_KEY_ATTR_SKB_MARK:
  442. case OVS_KEY_ATTR_ETHERNET:
  443. break;
  444. case OVS_KEY_ATTR_IPV4:
  445. if (flow_key->eth.type != htons(ETH_P_IP))
  446. return -EINVAL;
  447. if (!flow_key->ip.proto)
  448. return -EINVAL;
  449. ipv4_key = nla_data(ovs_key);
  450. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  451. return -EINVAL;
  452. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  453. return -EINVAL;
  454. break;
  455. case OVS_KEY_ATTR_IPV6:
  456. if (flow_key->eth.type != htons(ETH_P_IPV6))
  457. return -EINVAL;
  458. if (!flow_key->ip.proto)
  459. return -EINVAL;
  460. ipv6_key = nla_data(ovs_key);
  461. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  462. return -EINVAL;
  463. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  464. return -EINVAL;
  465. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  466. return -EINVAL;
  467. break;
  468. case OVS_KEY_ATTR_TCP:
  469. if (flow_key->ip.proto != IPPROTO_TCP)
  470. return -EINVAL;
  471. return validate_tp_port(flow_key);
  472. case OVS_KEY_ATTR_UDP:
  473. if (flow_key->ip.proto != IPPROTO_UDP)
  474. return -EINVAL;
  475. return validate_tp_port(flow_key);
  476. default:
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. static int validate_userspace(const struct nlattr *attr)
  482. {
  483. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  484. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  485. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  486. };
  487. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  488. int error;
  489. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  490. attr, userspace_policy);
  491. if (error)
  492. return error;
  493. if (!a[OVS_USERSPACE_ATTR_PID] ||
  494. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  495. return -EINVAL;
  496. return 0;
  497. }
  498. static int validate_actions(const struct nlattr *attr,
  499. const struct sw_flow_key *key, int depth)
  500. {
  501. const struct nlattr *a;
  502. int rem, err;
  503. if (depth >= SAMPLE_ACTION_DEPTH)
  504. return -EOVERFLOW;
  505. nla_for_each_nested(a, attr, rem) {
  506. /* Expected argument lengths, (u32)-1 for variable length. */
  507. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  508. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  509. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  510. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  511. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  512. [OVS_ACTION_ATTR_SET] = (u32)-1,
  513. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  514. };
  515. const struct ovs_action_push_vlan *vlan;
  516. int type = nla_type(a);
  517. if (type > OVS_ACTION_ATTR_MAX ||
  518. (action_lens[type] != nla_len(a) &&
  519. action_lens[type] != (u32)-1))
  520. return -EINVAL;
  521. switch (type) {
  522. case OVS_ACTION_ATTR_UNSPEC:
  523. return -EINVAL;
  524. case OVS_ACTION_ATTR_USERSPACE:
  525. err = validate_userspace(a);
  526. if (err)
  527. return err;
  528. break;
  529. case OVS_ACTION_ATTR_OUTPUT:
  530. if (nla_get_u32(a) >= DP_MAX_PORTS)
  531. return -EINVAL;
  532. break;
  533. case OVS_ACTION_ATTR_POP_VLAN:
  534. break;
  535. case OVS_ACTION_ATTR_PUSH_VLAN:
  536. vlan = nla_data(a);
  537. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  538. return -EINVAL;
  539. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  540. return -EINVAL;
  541. break;
  542. case OVS_ACTION_ATTR_SET:
  543. err = validate_set(a, key);
  544. if (err)
  545. return err;
  546. break;
  547. case OVS_ACTION_ATTR_SAMPLE:
  548. err = validate_sample(a, key, depth);
  549. if (err)
  550. return err;
  551. break;
  552. default:
  553. return -EINVAL;
  554. }
  555. }
  556. if (rem > 0)
  557. return -EINVAL;
  558. return 0;
  559. }
  560. static void clear_stats(struct sw_flow *flow)
  561. {
  562. flow->used = 0;
  563. flow->tcp_flags = 0;
  564. flow->packet_count = 0;
  565. flow->byte_count = 0;
  566. }
  567. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  568. {
  569. struct ovs_header *ovs_header = info->userhdr;
  570. struct nlattr **a = info->attrs;
  571. struct sw_flow_actions *acts;
  572. struct sk_buff *packet;
  573. struct sw_flow *flow;
  574. struct datapath *dp;
  575. struct ethhdr *eth;
  576. int len;
  577. int err;
  578. int key_len;
  579. err = -EINVAL;
  580. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  581. !a[OVS_PACKET_ATTR_ACTIONS])
  582. goto err;
  583. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  584. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  585. err = -ENOMEM;
  586. if (!packet)
  587. goto err;
  588. skb_reserve(packet, NET_IP_ALIGN);
  589. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  590. skb_reset_mac_header(packet);
  591. eth = eth_hdr(packet);
  592. /* Normally, setting the skb 'protocol' field would be handled by a
  593. * call to eth_type_trans(), but it assumes there's a sending
  594. * device, which we may not have. */
  595. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  596. packet->protocol = eth->h_proto;
  597. else
  598. packet->protocol = htons(ETH_P_802_2);
  599. /* Build an sw_flow for sending this packet. */
  600. flow = ovs_flow_alloc();
  601. err = PTR_ERR(flow);
  602. if (IS_ERR(flow))
  603. goto err_kfree_skb;
  604. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  605. if (err)
  606. goto err_flow_free;
  607. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  608. &flow->key.phy.skb_mark,
  609. &flow->key.phy.in_port,
  610. a[OVS_PACKET_ATTR_KEY]);
  611. if (err)
  612. goto err_flow_free;
  613. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  614. if (err)
  615. goto err_flow_free;
  616. flow->hash = ovs_flow_hash(&flow->key, key_len);
  617. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  618. err = PTR_ERR(acts);
  619. if (IS_ERR(acts))
  620. goto err_flow_free;
  621. rcu_assign_pointer(flow->sf_acts, acts);
  622. OVS_CB(packet)->flow = flow;
  623. packet->priority = flow->key.phy.priority;
  624. packet->mark = flow->key.phy.skb_mark;
  625. rcu_read_lock();
  626. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  627. err = -ENODEV;
  628. if (!dp)
  629. goto err_unlock;
  630. local_bh_disable();
  631. err = ovs_execute_actions(dp, packet);
  632. local_bh_enable();
  633. rcu_read_unlock();
  634. ovs_flow_free(flow);
  635. return err;
  636. err_unlock:
  637. rcu_read_unlock();
  638. err_flow_free:
  639. ovs_flow_free(flow);
  640. err_kfree_skb:
  641. kfree_skb(packet);
  642. err:
  643. return err;
  644. }
  645. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  646. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  647. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  648. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  649. };
  650. static struct genl_ops dp_packet_genl_ops[] = {
  651. { .cmd = OVS_PACKET_CMD_EXECUTE,
  652. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  653. .policy = packet_policy,
  654. .doit = ovs_packet_cmd_execute
  655. }
  656. };
  657. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  658. {
  659. int i;
  660. struct flow_table *table = ovsl_dereference(dp->table);
  661. stats->n_flows = ovs_flow_tbl_count(table);
  662. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  663. for_each_possible_cpu(i) {
  664. const struct dp_stats_percpu *percpu_stats;
  665. struct dp_stats_percpu local_stats;
  666. unsigned int start;
  667. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  668. do {
  669. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  670. local_stats = *percpu_stats;
  671. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  672. stats->n_hit += local_stats.n_hit;
  673. stats->n_missed += local_stats.n_missed;
  674. stats->n_lost += local_stats.n_lost;
  675. }
  676. }
  677. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  678. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  679. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  680. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  681. };
  682. static struct genl_family dp_flow_genl_family = {
  683. .id = GENL_ID_GENERATE,
  684. .hdrsize = sizeof(struct ovs_header),
  685. .name = OVS_FLOW_FAMILY,
  686. .version = OVS_FLOW_VERSION,
  687. .maxattr = OVS_FLOW_ATTR_MAX,
  688. .netnsok = true,
  689. .parallel_ops = true,
  690. };
  691. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  692. .name = OVS_FLOW_MCGROUP
  693. };
  694. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  695. {
  696. return NLMSG_ALIGN(sizeof(struct ovs_header))
  697. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  698. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  699. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  700. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  701. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  702. }
  703. /* Called with ovs_mutex. */
  704. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  705. struct sk_buff *skb, u32 portid,
  706. u32 seq, u32 flags, u8 cmd)
  707. {
  708. const int skb_orig_len = skb->len;
  709. const struct sw_flow_actions *sf_acts;
  710. struct ovs_flow_stats stats;
  711. struct ovs_header *ovs_header;
  712. struct nlattr *nla;
  713. unsigned long used;
  714. u8 tcp_flags;
  715. int err;
  716. sf_acts = ovsl_dereference(flow->sf_acts);
  717. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  718. if (!ovs_header)
  719. return -EMSGSIZE;
  720. ovs_header->dp_ifindex = get_dpifindex(dp);
  721. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  722. if (!nla)
  723. goto nla_put_failure;
  724. err = ovs_flow_to_nlattrs(&flow->key, skb);
  725. if (err)
  726. goto error;
  727. nla_nest_end(skb, nla);
  728. spin_lock_bh(&flow->lock);
  729. used = flow->used;
  730. stats.n_packets = flow->packet_count;
  731. stats.n_bytes = flow->byte_count;
  732. tcp_flags = flow->tcp_flags;
  733. spin_unlock_bh(&flow->lock);
  734. if (used &&
  735. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  736. goto nla_put_failure;
  737. if (stats.n_packets &&
  738. nla_put(skb, OVS_FLOW_ATTR_STATS,
  739. sizeof(struct ovs_flow_stats), &stats))
  740. goto nla_put_failure;
  741. if (tcp_flags &&
  742. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  743. goto nla_put_failure;
  744. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  745. * this is the first flow to be dumped into 'skb'. This is unusual for
  746. * Netlink but individual action lists can be longer than
  747. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  748. * The userspace caller can always fetch the actions separately if it
  749. * really wants them. (Most userspace callers in fact don't care.)
  750. *
  751. * This can only fail for dump operations because the skb is always
  752. * properly sized for single flows.
  753. */
  754. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  755. sf_acts->actions);
  756. if (err < 0 && skb_orig_len)
  757. goto error;
  758. return genlmsg_end(skb, ovs_header);
  759. nla_put_failure:
  760. err = -EMSGSIZE;
  761. error:
  762. genlmsg_cancel(skb, ovs_header);
  763. return err;
  764. }
  765. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  766. {
  767. const struct sw_flow_actions *sf_acts;
  768. sf_acts = ovsl_dereference(flow->sf_acts);
  769. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  770. }
  771. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  772. struct datapath *dp,
  773. u32 portid, u32 seq, u8 cmd)
  774. {
  775. struct sk_buff *skb;
  776. int retval;
  777. skb = ovs_flow_cmd_alloc_info(flow);
  778. if (!skb)
  779. return ERR_PTR(-ENOMEM);
  780. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  781. BUG_ON(retval < 0);
  782. return skb;
  783. }
  784. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  785. {
  786. struct nlattr **a = info->attrs;
  787. struct ovs_header *ovs_header = info->userhdr;
  788. struct sw_flow_key key;
  789. struct sw_flow *flow;
  790. struct sk_buff *reply;
  791. struct datapath *dp;
  792. struct flow_table *table;
  793. int error;
  794. int key_len;
  795. /* Extract key. */
  796. error = -EINVAL;
  797. if (!a[OVS_FLOW_ATTR_KEY])
  798. goto error;
  799. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  800. if (error)
  801. goto error;
  802. /* Validate actions. */
  803. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  804. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  805. if (error)
  806. goto error;
  807. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  808. error = -EINVAL;
  809. goto error;
  810. }
  811. ovs_lock();
  812. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  813. error = -ENODEV;
  814. if (!dp)
  815. goto err_unlock_ovs;
  816. table = ovsl_dereference(dp->table);
  817. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  818. if (!flow) {
  819. struct sw_flow_actions *acts;
  820. /* Bail out if we're not allowed to create a new flow. */
  821. error = -ENOENT;
  822. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  823. goto err_unlock_ovs;
  824. /* Expand table, if necessary, to make room. */
  825. if (ovs_flow_tbl_need_to_expand(table)) {
  826. struct flow_table *new_table;
  827. new_table = ovs_flow_tbl_expand(table);
  828. if (!IS_ERR(new_table)) {
  829. rcu_assign_pointer(dp->table, new_table);
  830. ovs_flow_tbl_deferred_destroy(table);
  831. table = ovsl_dereference(dp->table);
  832. }
  833. }
  834. /* Allocate flow. */
  835. flow = ovs_flow_alloc();
  836. if (IS_ERR(flow)) {
  837. error = PTR_ERR(flow);
  838. goto err_unlock_ovs;
  839. }
  840. flow->key = key;
  841. clear_stats(flow);
  842. /* Obtain actions. */
  843. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  844. error = PTR_ERR(acts);
  845. if (IS_ERR(acts))
  846. goto error_free_flow;
  847. rcu_assign_pointer(flow->sf_acts, acts);
  848. /* Put flow in bucket. */
  849. flow->hash = ovs_flow_hash(&key, key_len);
  850. ovs_flow_tbl_insert(table, flow);
  851. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  852. info->snd_seq,
  853. OVS_FLOW_CMD_NEW);
  854. } else {
  855. /* We found a matching flow. */
  856. struct sw_flow_actions *old_acts;
  857. struct nlattr *acts_attrs;
  858. /* Bail out if we're not allowed to modify an existing flow.
  859. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  860. * because Generic Netlink treats the latter as a dump
  861. * request. We also accept NLM_F_EXCL in case that bug ever
  862. * gets fixed.
  863. */
  864. error = -EEXIST;
  865. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  866. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  867. goto err_unlock_ovs;
  868. /* Update actions. */
  869. old_acts = ovsl_dereference(flow->sf_acts);
  870. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  871. if (acts_attrs &&
  872. (old_acts->actions_len != nla_len(acts_attrs) ||
  873. memcmp(old_acts->actions, nla_data(acts_attrs),
  874. old_acts->actions_len))) {
  875. struct sw_flow_actions *new_acts;
  876. new_acts = ovs_flow_actions_alloc(acts_attrs);
  877. error = PTR_ERR(new_acts);
  878. if (IS_ERR(new_acts))
  879. goto err_unlock_ovs;
  880. rcu_assign_pointer(flow->sf_acts, new_acts);
  881. ovs_flow_deferred_free_acts(old_acts);
  882. }
  883. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  884. info->snd_seq, OVS_FLOW_CMD_NEW);
  885. /* Clear stats. */
  886. if (a[OVS_FLOW_ATTR_CLEAR]) {
  887. spin_lock_bh(&flow->lock);
  888. clear_stats(flow);
  889. spin_unlock_bh(&flow->lock);
  890. }
  891. }
  892. ovs_unlock();
  893. if (!IS_ERR(reply))
  894. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  895. else
  896. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  897. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  898. return 0;
  899. error_free_flow:
  900. ovs_flow_free(flow);
  901. err_unlock_ovs:
  902. ovs_unlock();
  903. error:
  904. return error;
  905. }
  906. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  907. {
  908. struct nlattr **a = info->attrs;
  909. struct ovs_header *ovs_header = info->userhdr;
  910. struct sw_flow_key key;
  911. struct sk_buff *reply;
  912. struct sw_flow *flow;
  913. struct datapath *dp;
  914. struct flow_table *table;
  915. int err;
  916. int key_len;
  917. if (!a[OVS_FLOW_ATTR_KEY])
  918. return -EINVAL;
  919. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  920. if (err)
  921. return err;
  922. ovs_lock();
  923. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  924. if (!dp) {
  925. err = -ENODEV;
  926. goto unlock;
  927. }
  928. table = ovsl_dereference(dp->table);
  929. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  930. if (!flow) {
  931. err = -ENOENT;
  932. goto unlock;
  933. }
  934. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  935. info->snd_seq, OVS_FLOW_CMD_NEW);
  936. if (IS_ERR(reply)) {
  937. err = PTR_ERR(reply);
  938. goto unlock;
  939. }
  940. ovs_unlock();
  941. return genlmsg_reply(reply, info);
  942. unlock:
  943. ovs_unlock();
  944. return err;
  945. }
  946. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  947. {
  948. struct nlattr **a = info->attrs;
  949. struct ovs_header *ovs_header = info->userhdr;
  950. struct sw_flow_key key;
  951. struct sk_buff *reply;
  952. struct sw_flow *flow;
  953. struct datapath *dp;
  954. struct flow_table *table;
  955. int err;
  956. int key_len;
  957. ovs_lock();
  958. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  959. if (!dp) {
  960. err = -ENODEV;
  961. goto unlock;
  962. }
  963. if (!a[OVS_FLOW_ATTR_KEY]) {
  964. err = flush_flows(dp);
  965. goto unlock;
  966. }
  967. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  968. if (err)
  969. goto unlock;
  970. table = ovsl_dereference(dp->table);
  971. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  972. if (!flow) {
  973. err = -ENOENT;
  974. goto unlock;
  975. }
  976. reply = ovs_flow_cmd_alloc_info(flow);
  977. if (!reply) {
  978. err = -ENOMEM;
  979. goto unlock;
  980. }
  981. ovs_flow_tbl_remove(table, flow);
  982. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  983. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  984. BUG_ON(err < 0);
  985. ovs_flow_deferred_free(flow);
  986. ovs_unlock();
  987. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  988. return 0;
  989. unlock:
  990. ovs_unlock();
  991. return err;
  992. }
  993. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  994. {
  995. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  996. struct datapath *dp;
  997. struct flow_table *table;
  998. ovs_lock();
  999. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1000. if (!dp) {
  1001. ovs_unlock();
  1002. return -ENODEV;
  1003. }
  1004. table = ovsl_dereference(dp->table);
  1005. for (;;) {
  1006. struct sw_flow *flow;
  1007. u32 bucket, obj;
  1008. bucket = cb->args[0];
  1009. obj = cb->args[1];
  1010. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  1011. if (!flow)
  1012. break;
  1013. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  1014. NETLINK_CB(cb->skb).portid,
  1015. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1016. OVS_FLOW_CMD_NEW) < 0)
  1017. break;
  1018. cb->args[0] = bucket;
  1019. cb->args[1] = obj;
  1020. }
  1021. ovs_unlock();
  1022. return skb->len;
  1023. }
  1024. static struct genl_ops dp_flow_genl_ops[] = {
  1025. { .cmd = OVS_FLOW_CMD_NEW,
  1026. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1027. .policy = flow_policy,
  1028. .doit = ovs_flow_cmd_new_or_set
  1029. },
  1030. { .cmd = OVS_FLOW_CMD_DEL,
  1031. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1032. .policy = flow_policy,
  1033. .doit = ovs_flow_cmd_del
  1034. },
  1035. { .cmd = OVS_FLOW_CMD_GET,
  1036. .flags = 0, /* OK for unprivileged users. */
  1037. .policy = flow_policy,
  1038. .doit = ovs_flow_cmd_get,
  1039. .dumpit = ovs_flow_cmd_dump
  1040. },
  1041. { .cmd = OVS_FLOW_CMD_SET,
  1042. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1043. .policy = flow_policy,
  1044. .doit = ovs_flow_cmd_new_or_set,
  1045. },
  1046. };
  1047. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1048. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1049. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1050. };
  1051. static struct genl_family dp_datapath_genl_family = {
  1052. .id = GENL_ID_GENERATE,
  1053. .hdrsize = sizeof(struct ovs_header),
  1054. .name = OVS_DATAPATH_FAMILY,
  1055. .version = OVS_DATAPATH_VERSION,
  1056. .maxattr = OVS_DP_ATTR_MAX,
  1057. .netnsok = true,
  1058. .parallel_ops = true,
  1059. };
  1060. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1061. .name = OVS_DATAPATH_MCGROUP
  1062. };
  1063. static size_t ovs_dp_cmd_msg_size(void)
  1064. {
  1065. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1066. msgsize += nla_total_size(IFNAMSIZ);
  1067. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1068. return msgsize;
  1069. }
  1070. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1071. u32 portid, u32 seq, u32 flags, u8 cmd)
  1072. {
  1073. struct ovs_header *ovs_header;
  1074. struct ovs_dp_stats dp_stats;
  1075. int err;
  1076. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1077. flags, cmd);
  1078. if (!ovs_header)
  1079. goto error;
  1080. ovs_header->dp_ifindex = get_dpifindex(dp);
  1081. rcu_read_lock();
  1082. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1083. rcu_read_unlock();
  1084. if (err)
  1085. goto nla_put_failure;
  1086. get_dp_stats(dp, &dp_stats);
  1087. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1088. goto nla_put_failure;
  1089. return genlmsg_end(skb, ovs_header);
  1090. nla_put_failure:
  1091. genlmsg_cancel(skb, ovs_header);
  1092. error:
  1093. return -EMSGSIZE;
  1094. }
  1095. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1096. u32 seq, u8 cmd)
  1097. {
  1098. struct sk_buff *skb;
  1099. int retval;
  1100. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1101. if (!skb)
  1102. return ERR_PTR(-ENOMEM);
  1103. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1104. if (retval < 0) {
  1105. kfree_skb(skb);
  1106. return ERR_PTR(retval);
  1107. }
  1108. return skb;
  1109. }
  1110. /* Called with ovs_mutex. */
  1111. static struct datapath *lookup_datapath(struct net *net,
  1112. struct ovs_header *ovs_header,
  1113. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1114. {
  1115. struct datapath *dp;
  1116. if (!a[OVS_DP_ATTR_NAME])
  1117. dp = get_dp(net, ovs_header->dp_ifindex);
  1118. else {
  1119. struct vport *vport;
  1120. rcu_read_lock();
  1121. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1122. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1123. rcu_read_unlock();
  1124. }
  1125. return dp ? dp : ERR_PTR(-ENODEV);
  1126. }
  1127. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1128. {
  1129. struct nlattr **a = info->attrs;
  1130. struct vport_parms parms;
  1131. struct sk_buff *reply;
  1132. struct datapath *dp;
  1133. struct vport *vport;
  1134. struct ovs_net *ovs_net;
  1135. int err, i;
  1136. err = -EINVAL;
  1137. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1138. goto err;
  1139. ovs_lock();
  1140. err = -ENOMEM;
  1141. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1142. if (dp == NULL)
  1143. goto err_unlock_ovs;
  1144. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1145. /* Allocate table. */
  1146. err = -ENOMEM;
  1147. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1148. if (!dp->table)
  1149. goto err_free_dp;
  1150. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1151. if (!dp->stats_percpu) {
  1152. err = -ENOMEM;
  1153. goto err_destroy_table;
  1154. }
  1155. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1156. GFP_KERNEL);
  1157. if (!dp->ports) {
  1158. err = -ENOMEM;
  1159. goto err_destroy_percpu;
  1160. }
  1161. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1162. INIT_HLIST_HEAD(&dp->ports[i]);
  1163. /* Set up our datapath device. */
  1164. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1165. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1166. parms.options = NULL;
  1167. parms.dp = dp;
  1168. parms.port_no = OVSP_LOCAL;
  1169. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1170. vport = new_vport(&parms);
  1171. if (IS_ERR(vport)) {
  1172. err = PTR_ERR(vport);
  1173. if (err == -EBUSY)
  1174. err = -EEXIST;
  1175. goto err_destroy_ports_array;
  1176. }
  1177. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1178. info->snd_seq, OVS_DP_CMD_NEW);
  1179. err = PTR_ERR(reply);
  1180. if (IS_ERR(reply))
  1181. goto err_destroy_local_port;
  1182. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1183. list_add_tail(&dp->list_node, &ovs_net->dps);
  1184. ovs_unlock();
  1185. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1186. return 0;
  1187. err_destroy_local_port:
  1188. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1189. err_destroy_ports_array:
  1190. kfree(dp->ports);
  1191. err_destroy_percpu:
  1192. free_percpu(dp->stats_percpu);
  1193. err_destroy_table:
  1194. ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
  1195. err_free_dp:
  1196. release_net(ovs_dp_get_net(dp));
  1197. kfree(dp);
  1198. err_unlock_ovs:
  1199. ovs_unlock();
  1200. err:
  1201. return err;
  1202. }
  1203. /* Called with ovs_mutex. */
  1204. static void __dp_destroy(struct datapath *dp)
  1205. {
  1206. int i;
  1207. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1208. struct vport *vport;
  1209. struct hlist_node *n;
  1210. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1211. if (vport->port_no != OVSP_LOCAL)
  1212. ovs_dp_detach_port(vport);
  1213. }
  1214. list_del(&dp->list_node);
  1215. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1216. * all port in datapath are destroyed first before freeing datapath.
  1217. */
  1218. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1219. call_rcu(&dp->rcu, destroy_dp_rcu);
  1220. }
  1221. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1222. {
  1223. struct sk_buff *reply;
  1224. struct datapath *dp;
  1225. int err;
  1226. ovs_lock();
  1227. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1228. err = PTR_ERR(dp);
  1229. if (IS_ERR(dp))
  1230. goto unlock;
  1231. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1232. info->snd_seq, OVS_DP_CMD_DEL);
  1233. err = PTR_ERR(reply);
  1234. if (IS_ERR(reply))
  1235. goto unlock;
  1236. __dp_destroy(dp);
  1237. ovs_unlock();
  1238. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1239. return 0;
  1240. unlock:
  1241. ovs_unlock();
  1242. return err;
  1243. }
  1244. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1245. {
  1246. struct sk_buff *reply;
  1247. struct datapath *dp;
  1248. int err;
  1249. ovs_lock();
  1250. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1251. err = PTR_ERR(dp);
  1252. if (IS_ERR(dp))
  1253. goto unlock;
  1254. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1255. info->snd_seq, OVS_DP_CMD_NEW);
  1256. if (IS_ERR(reply)) {
  1257. err = PTR_ERR(reply);
  1258. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1259. ovs_dp_datapath_multicast_group.id, err);
  1260. err = 0;
  1261. goto unlock;
  1262. }
  1263. ovs_unlock();
  1264. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1265. return 0;
  1266. unlock:
  1267. ovs_unlock();
  1268. return err;
  1269. }
  1270. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1271. {
  1272. struct sk_buff *reply;
  1273. struct datapath *dp;
  1274. int err;
  1275. ovs_lock();
  1276. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1277. if (IS_ERR(dp)) {
  1278. err = PTR_ERR(dp);
  1279. goto unlock;
  1280. }
  1281. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1282. info->snd_seq, OVS_DP_CMD_NEW);
  1283. if (IS_ERR(reply)) {
  1284. err = PTR_ERR(reply);
  1285. goto unlock;
  1286. }
  1287. ovs_unlock();
  1288. return genlmsg_reply(reply, info);
  1289. unlock:
  1290. ovs_unlock();
  1291. return err;
  1292. }
  1293. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1294. {
  1295. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1296. struct datapath *dp;
  1297. int skip = cb->args[0];
  1298. int i = 0;
  1299. ovs_lock();
  1300. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1301. if (i >= skip &&
  1302. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1303. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1304. OVS_DP_CMD_NEW) < 0)
  1305. break;
  1306. i++;
  1307. }
  1308. ovs_unlock();
  1309. cb->args[0] = i;
  1310. return skb->len;
  1311. }
  1312. static struct genl_ops dp_datapath_genl_ops[] = {
  1313. { .cmd = OVS_DP_CMD_NEW,
  1314. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1315. .policy = datapath_policy,
  1316. .doit = ovs_dp_cmd_new
  1317. },
  1318. { .cmd = OVS_DP_CMD_DEL,
  1319. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1320. .policy = datapath_policy,
  1321. .doit = ovs_dp_cmd_del
  1322. },
  1323. { .cmd = OVS_DP_CMD_GET,
  1324. .flags = 0, /* OK for unprivileged users. */
  1325. .policy = datapath_policy,
  1326. .doit = ovs_dp_cmd_get,
  1327. .dumpit = ovs_dp_cmd_dump
  1328. },
  1329. { .cmd = OVS_DP_CMD_SET,
  1330. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1331. .policy = datapath_policy,
  1332. .doit = ovs_dp_cmd_set,
  1333. },
  1334. };
  1335. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1336. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1337. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1338. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1339. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1340. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1341. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1342. };
  1343. static struct genl_family dp_vport_genl_family = {
  1344. .id = GENL_ID_GENERATE,
  1345. .hdrsize = sizeof(struct ovs_header),
  1346. .name = OVS_VPORT_FAMILY,
  1347. .version = OVS_VPORT_VERSION,
  1348. .maxattr = OVS_VPORT_ATTR_MAX,
  1349. .netnsok = true,
  1350. .parallel_ops = true,
  1351. };
  1352. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1353. .name = OVS_VPORT_MCGROUP
  1354. };
  1355. /* Called with ovs_mutex or RCU read lock. */
  1356. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1357. u32 portid, u32 seq, u32 flags, u8 cmd)
  1358. {
  1359. struct ovs_header *ovs_header;
  1360. struct ovs_vport_stats vport_stats;
  1361. int err;
  1362. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1363. flags, cmd);
  1364. if (!ovs_header)
  1365. return -EMSGSIZE;
  1366. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1367. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1368. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1369. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1370. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1371. goto nla_put_failure;
  1372. ovs_vport_get_stats(vport, &vport_stats);
  1373. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1374. &vport_stats))
  1375. goto nla_put_failure;
  1376. err = ovs_vport_get_options(vport, skb);
  1377. if (err == -EMSGSIZE)
  1378. goto error;
  1379. return genlmsg_end(skb, ovs_header);
  1380. nla_put_failure:
  1381. err = -EMSGSIZE;
  1382. error:
  1383. genlmsg_cancel(skb, ovs_header);
  1384. return err;
  1385. }
  1386. /* Called with ovs_mutex or RCU read lock. */
  1387. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1388. u32 seq, u8 cmd)
  1389. {
  1390. struct sk_buff *skb;
  1391. int retval;
  1392. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1393. if (!skb)
  1394. return ERR_PTR(-ENOMEM);
  1395. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1396. BUG_ON(retval < 0);
  1397. return skb;
  1398. }
  1399. /* Called with ovs_mutex or RCU read lock. */
  1400. static struct vport *lookup_vport(struct net *net,
  1401. struct ovs_header *ovs_header,
  1402. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1403. {
  1404. struct datapath *dp;
  1405. struct vport *vport;
  1406. if (a[OVS_VPORT_ATTR_NAME]) {
  1407. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1408. if (!vport)
  1409. return ERR_PTR(-ENODEV);
  1410. if (ovs_header->dp_ifindex &&
  1411. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1412. return ERR_PTR(-ENODEV);
  1413. return vport;
  1414. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1415. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1416. if (port_no >= DP_MAX_PORTS)
  1417. return ERR_PTR(-EFBIG);
  1418. dp = get_dp(net, ovs_header->dp_ifindex);
  1419. if (!dp)
  1420. return ERR_PTR(-ENODEV);
  1421. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1422. if (!vport)
  1423. return ERR_PTR(-ENODEV);
  1424. return vport;
  1425. } else
  1426. return ERR_PTR(-EINVAL);
  1427. }
  1428. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1429. {
  1430. struct nlattr **a = info->attrs;
  1431. struct ovs_header *ovs_header = info->userhdr;
  1432. struct vport_parms parms;
  1433. struct sk_buff *reply;
  1434. struct vport *vport;
  1435. struct datapath *dp;
  1436. u32 port_no;
  1437. int err;
  1438. err = -EINVAL;
  1439. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1440. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1441. goto exit;
  1442. ovs_lock();
  1443. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1444. err = -ENODEV;
  1445. if (!dp)
  1446. goto exit_unlock;
  1447. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1448. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1449. err = -EFBIG;
  1450. if (port_no >= DP_MAX_PORTS)
  1451. goto exit_unlock;
  1452. vport = ovs_vport_ovsl(dp, port_no);
  1453. err = -EBUSY;
  1454. if (vport)
  1455. goto exit_unlock;
  1456. } else {
  1457. for (port_no = 1; ; port_no++) {
  1458. if (port_no >= DP_MAX_PORTS) {
  1459. err = -EFBIG;
  1460. goto exit_unlock;
  1461. }
  1462. vport = ovs_vport_ovsl(dp, port_no);
  1463. if (!vport)
  1464. break;
  1465. }
  1466. }
  1467. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1468. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1469. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1470. parms.dp = dp;
  1471. parms.port_no = port_no;
  1472. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1473. vport = new_vport(&parms);
  1474. err = PTR_ERR(vport);
  1475. if (IS_ERR(vport))
  1476. goto exit_unlock;
  1477. err = 0;
  1478. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1479. OVS_VPORT_CMD_NEW);
  1480. if (IS_ERR(reply)) {
  1481. err = PTR_ERR(reply);
  1482. ovs_dp_detach_port(vport);
  1483. goto exit_unlock;
  1484. }
  1485. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1486. exit_unlock:
  1487. ovs_unlock();
  1488. exit:
  1489. return err;
  1490. }
  1491. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1492. {
  1493. struct nlattr **a = info->attrs;
  1494. struct sk_buff *reply;
  1495. struct vport *vport;
  1496. int err;
  1497. ovs_lock();
  1498. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1499. err = PTR_ERR(vport);
  1500. if (IS_ERR(vport))
  1501. goto exit_unlock;
  1502. err = 0;
  1503. if (a[OVS_VPORT_ATTR_TYPE] &&
  1504. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1505. err = -EINVAL;
  1506. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1507. if (!reply) {
  1508. err = -ENOMEM;
  1509. goto exit_unlock;
  1510. }
  1511. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1512. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1513. if (err)
  1514. goto exit_free;
  1515. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1516. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1517. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1518. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1519. BUG_ON(err < 0);
  1520. ovs_unlock();
  1521. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1522. return 0;
  1523. rtnl_unlock();
  1524. return 0;
  1525. exit_free:
  1526. kfree_skb(reply);
  1527. exit_unlock:
  1528. ovs_unlock();
  1529. return err;
  1530. }
  1531. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1532. {
  1533. struct nlattr **a = info->attrs;
  1534. struct sk_buff *reply;
  1535. struct vport *vport;
  1536. int err;
  1537. ovs_lock();
  1538. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1539. err = PTR_ERR(vport);
  1540. if (IS_ERR(vport))
  1541. goto exit_unlock;
  1542. if (vport->port_no == OVSP_LOCAL) {
  1543. err = -EINVAL;
  1544. goto exit_unlock;
  1545. }
  1546. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1547. OVS_VPORT_CMD_DEL);
  1548. err = PTR_ERR(reply);
  1549. if (IS_ERR(reply))
  1550. goto exit_unlock;
  1551. err = 0;
  1552. ovs_dp_detach_port(vport);
  1553. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1554. exit_unlock:
  1555. ovs_unlock();
  1556. return err;
  1557. }
  1558. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1559. {
  1560. struct nlattr **a = info->attrs;
  1561. struct ovs_header *ovs_header = info->userhdr;
  1562. struct sk_buff *reply;
  1563. struct vport *vport;
  1564. int err;
  1565. rcu_read_lock();
  1566. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1567. err = PTR_ERR(vport);
  1568. if (IS_ERR(vport))
  1569. goto exit_unlock;
  1570. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1571. OVS_VPORT_CMD_NEW);
  1572. err = PTR_ERR(reply);
  1573. if (IS_ERR(reply))
  1574. goto exit_unlock;
  1575. rcu_read_unlock();
  1576. return genlmsg_reply(reply, info);
  1577. exit_unlock:
  1578. rcu_read_unlock();
  1579. return err;
  1580. }
  1581. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1582. {
  1583. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1584. struct datapath *dp;
  1585. int bucket = cb->args[0], skip = cb->args[1];
  1586. int i, j = 0;
  1587. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1588. if (!dp)
  1589. return -ENODEV;
  1590. rcu_read_lock();
  1591. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1592. struct vport *vport;
  1593. j = 0;
  1594. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1595. if (j >= skip &&
  1596. ovs_vport_cmd_fill_info(vport, skb,
  1597. NETLINK_CB(cb->skb).portid,
  1598. cb->nlh->nlmsg_seq,
  1599. NLM_F_MULTI,
  1600. OVS_VPORT_CMD_NEW) < 0)
  1601. goto out;
  1602. j++;
  1603. }
  1604. skip = 0;
  1605. }
  1606. out:
  1607. rcu_read_unlock();
  1608. cb->args[0] = i;
  1609. cb->args[1] = j;
  1610. return skb->len;
  1611. }
  1612. static struct genl_ops dp_vport_genl_ops[] = {
  1613. { .cmd = OVS_VPORT_CMD_NEW,
  1614. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1615. .policy = vport_policy,
  1616. .doit = ovs_vport_cmd_new
  1617. },
  1618. { .cmd = OVS_VPORT_CMD_DEL,
  1619. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1620. .policy = vport_policy,
  1621. .doit = ovs_vport_cmd_del
  1622. },
  1623. { .cmd = OVS_VPORT_CMD_GET,
  1624. .flags = 0, /* OK for unprivileged users. */
  1625. .policy = vport_policy,
  1626. .doit = ovs_vport_cmd_get,
  1627. .dumpit = ovs_vport_cmd_dump
  1628. },
  1629. { .cmd = OVS_VPORT_CMD_SET,
  1630. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1631. .policy = vport_policy,
  1632. .doit = ovs_vport_cmd_set,
  1633. },
  1634. };
  1635. struct genl_family_and_ops {
  1636. struct genl_family *family;
  1637. struct genl_ops *ops;
  1638. int n_ops;
  1639. struct genl_multicast_group *group;
  1640. };
  1641. static const struct genl_family_and_ops dp_genl_families[] = {
  1642. { &dp_datapath_genl_family,
  1643. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1644. &ovs_dp_datapath_multicast_group },
  1645. { &dp_vport_genl_family,
  1646. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1647. &ovs_dp_vport_multicast_group },
  1648. { &dp_flow_genl_family,
  1649. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1650. &ovs_dp_flow_multicast_group },
  1651. { &dp_packet_genl_family,
  1652. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1653. NULL },
  1654. };
  1655. static void dp_unregister_genl(int n_families)
  1656. {
  1657. int i;
  1658. for (i = 0; i < n_families; i++)
  1659. genl_unregister_family(dp_genl_families[i].family);
  1660. }
  1661. static int dp_register_genl(void)
  1662. {
  1663. int n_registered;
  1664. int err;
  1665. int i;
  1666. n_registered = 0;
  1667. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1668. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1669. err = genl_register_family_with_ops(f->family, f->ops,
  1670. f->n_ops);
  1671. if (err)
  1672. goto error;
  1673. n_registered++;
  1674. if (f->group) {
  1675. err = genl_register_mc_group(f->family, f->group);
  1676. if (err)
  1677. goto error;
  1678. }
  1679. }
  1680. return 0;
  1681. error:
  1682. dp_unregister_genl(n_registered);
  1683. return err;
  1684. }
  1685. static void rehash_flow_table(struct work_struct *work)
  1686. {
  1687. struct datapath *dp;
  1688. struct net *net;
  1689. ovs_lock();
  1690. rtnl_lock();
  1691. for_each_net(net) {
  1692. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1693. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1694. struct flow_table *old_table = ovsl_dereference(dp->table);
  1695. struct flow_table *new_table;
  1696. new_table = ovs_flow_tbl_rehash(old_table);
  1697. if (!IS_ERR(new_table)) {
  1698. rcu_assign_pointer(dp->table, new_table);
  1699. ovs_flow_tbl_deferred_destroy(old_table);
  1700. }
  1701. }
  1702. }
  1703. rtnl_unlock();
  1704. ovs_unlock();
  1705. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1706. }
  1707. static int __net_init ovs_init_net(struct net *net)
  1708. {
  1709. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1710. INIT_LIST_HEAD(&ovs_net->dps);
  1711. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1712. return 0;
  1713. }
  1714. static void __net_exit ovs_exit_net(struct net *net)
  1715. {
  1716. struct datapath *dp, *dp_next;
  1717. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1718. ovs_lock();
  1719. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1720. __dp_destroy(dp);
  1721. ovs_unlock();
  1722. cancel_work_sync(&ovs_net->dp_notify_work);
  1723. }
  1724. static struct pernet_operations ovs_net_ops = {
  1725. .init = ovs_init_net,
  1726. .exit = ovs_exit_net,
  1727. .id = &ovs_net_id,
  1728. .size = sizeof(struct ovs_net),
  1729. };
  1730. static int __init dp_init(void)
  1731. {
  1732. int err;
  1733. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1734. pr_info("Open vSwitch switching datapath\n");
  1735. err = ovs_flow_init();
  1736. if (err)
  1737. goto error;
  1738. err = ovs_vport_init();
  1739. if (err)
  1740. goto error_flow_exit;
  1741. err = register_pernet_device(&ovs_net_ops);
  1742. if (err)
  1743. goto error_vport_exit;
  1744. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1745. if (err)
  1746. goto error_netns_exit;
  1747. err = dp_register_genl();
  1748. if (err < 0)
  1749. goto error_unreg_notifier;
  1750. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1751. return 0;
  1752. error_unreg_notifier:
  1753. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1754. error_netns_exit:
  1755. unregister_pernet_device(&ovs_net_ops);
  1756. error_vport_exit:
  1757. ovs_vport_exit();
  1758. error_flow_exit:
  1759. ovs_flow_exit();
  1760. error:
  1761. return err;
  1762. }
  1763. static void dp_cleanup(void)
  1764. {
  1765. cancel_delayed_work_sync(&rehash_flow_wq);
  1766. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1767. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1768. unregister_pernet_device(&ovs_net_ops);
  1769. rcu_barrier();
  1770. ovs_vport_exit();
  1771. ovs_flow_exit();
  1772. }
  1773. module_init(dp_init);
  1774. module_exit(dp_cleanup);
  1775. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1776. MODULE_LICENSE("GPL");