datapath.c 48 KB

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